Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP memory_get_usage

I came across the PHP's memory_get_usage() and memory_get_peak_usage().

The problem is that I found that these two functions do not provide the real memory used by the current script. My test script is:

<?php

echo memory_get_usage();

echo '<br />';

$a = str_repeat('hello', 100000);

echo '<br />';

echo memory_get_usage();

echo '<br />';

echo memory_get_peak_usage();

?>

Which returns:

355120

5355216

5356008

What do you understand from this?

The first value is before executing the str_repeat() so it has to be the value of 0.

The second is after the process and it's OK to have a value greater than 0 but not that big value.

The third is the "peak" value and it's slightly greater than the second as I think it should be the biggest value in a processing microsecond.

So do you think that the real value of the current script's memory consumption should be like this:

memory_usage = the second memory usage - the first memory usage

peak_memory_usage = the third (peak_usage) - the first memory usage

which gives:

1) 5355216 - 355120 = 5000096 bytes

2) 5356008 - 355120 = 5000888 bytes

If this is how it works, I assume that the first 355120 bytes are the whole system allocated memory used by apache and other modules, as the first value never changes when you increase or decrease the number of repeats in the str_repeat(), only the two values after the process increase or decrease but never gets smaller that the first value.

like image 875
medk Avatar asked Oct 24 '10 22:10

medk


People also ask

How do I monitor PHP memory usage?

The memory_get_usage function can be used to track the memory usage. The 'malloc' function is not used for every block required, instead a big chunk of system memory is allocated and the environment variable is changed and managed internally.

What is the maximum PHP memory limit?

By default, a PHP script can allocate up to 128 megabytes of memory. To verify the current value of the memory_limit directive and other directives, you can use the phpinfo() function.

Does unset free memory PHP?

unset() does just what its name says - unset a variable. It does not force immediate memory freeing. PHP's garbage collector will do it when it see fits - by intention as soon, as those CPU cycles aren't needed anyway, or as late as before the script would run out of memory, whatever occurs first.

How can I get CPU and memory usage in PHP?

To get the current memory usage, we can use the memory_get_usage() function, and to get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.


2 Answers

According to the php manual, memory_get_usage returns the amount of memory allocated to php, not necessarily the amount being used.

like image 78
Sam Dufel Avatar answered Oct 03 '22 08:10

Sam Dufel


Ok, your first assertion that the first memory_get_usage() should be 0 is wrong. According to PHP's documentation:

Returns the amount of memory, in bytes, that's currently being allocated to your PHP script.

Your script is running, therefore it must have some memory allocated to it. The first call informs you of how much that is.

Your second assertion that str_repeat() should not use that much memory is not looking at the whole picture.

You have the string "hello" (which uses 5 bytes) repeated 100,000 times, for a total of 500,000 bytes...minimum. The question is, how did PHP perform this action? Did they use code such as this? (pseudocode):

s = ""
for(i=0; i<100000; i++)
    s += "hello"

This code would require that you reallocate a new string for each iteration of the for loop. Now I can't pretend to say that I know how PHP implements str_repeat(), but you have to be extremely careful with how you use memory to keep memory usage down. From the appearance of things, they did not manage memory in that function as well as they could have.

Third, the difference between the peak memory usage and current memory usage likely comes from the stack that was necessary to make the function call to str_repeat(), as well as any local variables necessary within that function. The memory was probably reclaimed when the function returned.

Finally, Apache runs in a different process and we are dealing with virtual memory. Nothing that Apache does will affect the result of memory_get_usage() as processes do not "share" virtual memory.

like image 34
riwalk Avatar answered Oct 03 '22 06:10

riwalk