Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP memory_get_usage is larger than memory_limit

My PHP application has been running a bit slow and it's not very memory efficient at the moment. My whole server has been going down very often and I think I have this app to blame. I thought I'd monitor the memory usage and check how much I have as a limit:

echo 'Memory in use: ' . memory_get_usage() . ' ('. memory_get_usage()/1024 .'M) <br>';
echo 'Peak usage: ' . memory_get_peak_usage() . ' ('. memory_get_peak_usage()/1024 .'M) <br>';
echo 'Memory limit: ' . ini_get('memory_limit') . '<br>';

This shows the following:

Memory in use: 629632 (614.921875M) 
Peak usage: 635696 (620.796875M) 
Memory limit: 128M

How could this be? Memory in use is WAY larger than memory limit? Either something's really broken or I do not understand at all how the memory_limit setting works (or memory_get_usage() )

Thank you all.

like image 556
Nahuel Avatar asked Aug 07 '13 13:08

Nahuel


People also ask

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. For more information, please see this article.

How to check PHP memory?

But how can you check how much memory your script is using? It's easy: the standard PHP library provides you with two functions that give you that exact information: memory_get_usage() and memory_get_peak_usage().

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. The above mentioned memory usage can be tracked using memory_get_usage().


1 Answers

memory_get_usage returns it in bytes, what you are calculating there is actually in kB. Divide it by 1024 again to have it in MB

Same goes for memory_get_peak_usage

e.g.

echo 'Memory in use: ' . memory_get_usage() . ' ('. ((memory_get_usage() / 1024) / 1024) .'M) <br>';
like image 94
AmazingDreams Avatar answered Sep 20 '22 02:09

AmazingDreams