PHP memory_get_usage is bigger than memory_limit

My PHP application is a little slower, and at the moment it is not very efficient. My whole server goes very often, and I think this application is to blame. I thought I would monitor the memory usage and check how much I have limited:

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? Is WAY memory usage larger than memory limit? Either something is really broken, or I don’t understand how the setting memory_limit (or memory_get_usage() ) works

Thanks to everyone.

+4
source share
1 answer

memory_get_usage returns it in bytes, what you calculate is actually in kB . Divide it by 1024 again so that it is in MB

The same goes for memory_get_peak_usage

eg.

 echo 'Memory in use: ' . memory_get_usage() . ' ('. ((memory_get_usage() / 1024) / 1024) .'M) <br>'; 
+7
source

Source: https://habr.com/ru/post/1495740/


All Articles