<commit_before># php-memprof

php-memprof profiles memory usage of PHP scripts, and especially can tell which
function has allocated every single byte of memory currently allocated.

This is different from measuring the memory usage before and after a
function call:

``` php
<?php

// script 1

function a() {
    $data = file_get_contents("huge-file");
}

a();

$profile = memprof_dump_array();

```

In script 1, a before/after approach would designate file_get_contents() as huge
memory consumer, while the memory it allocates is actually freed quickly after
it returns. When dumping the memory usage after a() returns, the memprof
approach would show that file_get_contents() is a small memory consumer since
the memory it allocated has been freed at the time memprof_dump_array() is
called.


``` php
<?php

// script 2

function a() {
    global $cache;
    $cache = file_get_contents("huge-file");
}

a();

$profile = memprof_dump_array();
```

In script 2, the allocated memory remains allocated after file_get_contents()
and a() return, and when memprof_dump_array() is called. This time a() and
file_get_contents() are shown as huge memory consumers.

## Dependencies

 * [Judy Library][3] (e.g. libjudy-dev or judy package)
 * C Library with [malloc hooks][1] (optional; allows to track persistent allocations too)

## Install

    phpize
    ./configure
    make
    make install

## Loading the extension

The extension can be loaded on the command line, just for one script:

    php -dextension=memprof.so script.php

Or permanently, in php.ini:

    extension=memprof.so

## Usage

Memprof can be enabled during script execution by calling ``memprof_enable()``.

Then the memory usage can be dumped by calling one of the ``memprof_dump_``
functions. Both tell which functions allocated all the currently allocated
memory.

Example:

```<commit_msg>Added pecl installation instructions<commit_after># php-memprof

php-memprof profiles memory usage of PHP scripts, and especially can tell which
function has allocated every single byte of memory currently allocated.

This is different from measuring the memory usage before and after a
function call:

``` php
<?php

// script 1

function a() {
    $data = file_get_contents("huge-file");
}

a();

$profile = memprof_dump_array();

```

In script 1, a before/after approach would designate file_get_contents() as huge
memory consumer, while the memory it allocates is actually freed quickly after
it returns. When dumping the memory usage after a() returns, the memprof
approach would show that file_get_contents() is a small memory consumer since
the memory it allocated has been freed at the time memprof_dump_array() is
called.


``` php
<?php

// script 2

function a() {
    global $cache;
    $cache = file_get_contents("huge-file");
}

a();

$profile = memprof_dump_array();
```

In script 2, the allocated memory remains allocated after file_get_contents()
and a() return, and when memprof_dump_array() is called. This time a() and
file_get_contents() are shown as huge memory consumers.

## Dependencies

 * [Judy Library][3] (e.g. libjudy-dev or judy package)
 * C Library with [malloc hooks][1] (optional; allows to track persistent allocations too)

## Install

### Using PECL

    pecl install memprof

### Manually

Download the source and run the following commands in the source directory:

    phpize
    ./configure
    make
    make install

## Loading the extension

The extension can be loaded on the command line, just for one script:

    php -dextension=memprof.so script.php

Or permanently, in php.ini:

    extension=memprof.so

## Usage

Memprof can be enabled during script execution by calling ``memprof_enable()``.

Then the memory usage can be dumped by calling one of the ``memprof_dump_``
functions. Both tell which functions allocated all the currently allocated
memory.

Example:

```
{'fraction_non_alphanumeric': 0.0815748841996912, 'fraction_numerical': 0.003088008234688626, 'mean_word_length': 4.295640326975477, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3151610', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1071, 'n_words': 493}