PHP performance Optimization process to avoid the need to get PHP memory consumption, using the Memory_get_usage () function to get the current memory consumption situation, the function is simple to use, the following example of the form of the use of Memory_get_usage () function to get the current usage of PHP memory consumption.
One, function prototype int memory_get_usage ([bool $real _usage=false])
Two, version compatible PHP 4 >= 4.3.2,php 5
Third, basic usage and Example 1, get current memory consumption
Echo Memory_get_usage ();
$var =str_repeat ("www.scutephp.com", 10000);
Echo Memory_get_usage ();
Unset ($var);
Echo Memory_get_usage ();
Result output: 62328 122504 62416
Description: The Memory_get_usage () function outputs a value of bytes Unit 2, and the formatted Memory_get_usage () result is output in kilobytes
$unit =array (' B ', ' KB ', ' MB ', ' GB ', ' TB ', ' PB ');
Return @round ($size/pow (1024x768, ($i =floor (log ($size, 1024))), 2). ' '. $unit [$i];
}
echo Convert (Memory_get_usage (true));
?>
Output: Kb3, custom function get array or variable value size
function Array_size ($arr) {
Ob_start ();
Print_r ($arr);
$mem =ob_get_contents ();
Ob_end_clean ();
$mem =preg_replace ("/\n +/", "", $mem);
$mem =strlen ($MEM);
return $mem;
$memEstimate =array_size ($GLOBALS);
?>
As you can see, to reduce the memory footprint, you can use the PHP unset () function to remove variables that you no longer need to use.
Similar to the following:
The PHP mysql_free_result () function allows you to empty the resulting set of query databases that you no longer need, so you can also get more usable memory.
PHP Memory_get_usage () can also have a parameter, $real _usage, whose value is a Boolean value. The default is FALSE, which indicates that the resulting memory usage does not include the memory used by the function (PHP memory manager), and when set to TRUE, the resulting memory is included in the memory used by the function (PHP memory manager). So in the actual programming, you can use PHP memory_get_usage () to compare the various methods to occupy the high and low memory, to choose the use of a small footprint method.