The Problem
Lets say I have this function:
function hog($i = 1) // uses $i * 0.5 MiB, returns $i * 0.25 MiB
{
$s = str_repeat('a', $i * 1024 * 512); return substr($s, $i * 1024 * 256);
}
I would like to call it and be able to inspect the maximum amount of memory it uses.
In other words: memory_get_function_peak_usage($callback);
. Is this possible?
What I Have Tried
I'm using the following values as my non-monotonically increasing $i
argument for hog()
:
$iterations = array_merge(range(0, 50, 10), range(50, 0, 5));
$iterations = array_fill_keys($iterations, 0);
Which is essentially:
(
[0] => 0
[10] => 0
[20] => 0
[30] => 0
[40] => 0
[50] => 0
[45] => 0
[35] => 0
[25] => 0
[15] => 0
[5] => 0
)
Enclosing with memory_get_usage()
foreach ($iterations as $key => $value)
{
$alpha = memory_get_usage(); hog($key);
$iterations[$key] = memory_get_usage() - $alpha;
}
print_r($iterations);
Output:
(
[0] => 96
[10] => 0
[20] => 0
[30] => 0
[40] => 0
[50] => 0
[45] => 0
[35] => 0
[25] => 0
[15] => 0
[5] => 0
)
If I store the return value of hog()
, the results start to look more realistic:
foreach ($iterations as $key => $value)
{
$alpha = memory_get_usage(); $s = hog($key);
$iterations[$key] = memory_get_usage() - $alpha; unset($s);
}
print_r($iterations);
Output:
(
[0] => 176
[10] => 2621536
[20] => 5242976
[30] => 7864416
[40] => 10485856
[50] => 13107296
[45] => 11796576
[35] => 9175136
[25] => 6553696
[15] => 3932256
[5] => 1310816
)
As expected, now it's showing me the amount of memory returned, but I need the total memory used.
Using register_tick_function()
:
I didn't knew, but it turns out that when you do:
declare (ticks=1)
{
$a = hog(1);
}
It won't tick for every line, statement or block of code inside of hog()
function, only for the code inside the declare
block - so, unless the function is defined within it, this option is a no go.
Mixing with gc_*
functions:
I tried (without much hope I must say) using combinations of gc_disable()
, gc_enable()
and gc_collect_cycles()
with both experiments above to see if anything changed - it didn't.
See Question&Answers more detail:
os