Memory Usage
suggest changePHP’s runtime memory limit is set through the INI directive memory_limit. This setting prevents any single execution of PHP from using up too much memory, exhausting it for other scripts and system software. The stock php.ini sets the limit to 128M. A distribution package may choose another value. Change it in php.ini or at runtime. -1 means no limit, which is a bad idea on a shared server.
The exact memory usage used during runtime can be determined by calling memory_get_usage(). It returns the number of bytes of memory allocated to the currently running script. As of PHP 5.2, it has one optional boolean parameter to get the total allocated system memory, as opposed to the memory that’s actively being used by PHP.
<?php
echo memory_get_usage() . "\n";
// Example from an older run: 350688. The number depends on the build.
// Let's use up some RAM
$array = array_fill(0, 1000, 'abc');
echo memory_get_usage() . "\n";
// Example: 387704
// Remove the array from memory
unset($array);
echo memory_get_usage() . "\n";
// Example: 350784
Now memory_get_usage gives you memory usage at the moment it is run. Between calls to this function you may allocate and deallocate other things in memory. To get the maximum amount of memory used up to a certain point, call memory_get_peak_usage().
<?php
echo memory_get_peak_usage() . "\n";
// 385688
$array = array_fill(0, 1000, 'abc');
echo memory_get_peak_usage() . "\n";
// 422736
unset($array);
echo memory_get_peak_usage() . "\n";
// 422776
Notice the value will only go up or stay constant.