1. Function prototype
int memory_get_usage ([ bool $real_usage = false ] )
2. Version compatible
PHP 4 >= 4.3. 2, PHP 5
3. Basic usage and examples
1. Get the current memory consumption
Copy code The code is as follows:
echo memory_get_usage();
$var = str_repeat("liuhui", 10000);
echo memory_get_usage();
unset($var);
echo memory_get_usage();
?>
Respectively output: 62328 122504 62416
Description: The value output by the memory_get_usage() function For bytes unit
2, format memory_get_usage() output
Copy the code The code is as follows:
< ;?php
function convert($size){
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
echo convert( memory_get_usage(true));
?>
Output: 256 kb
3, custom function to get the array or variable value size
Copy code The code is as follows:
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);
?>
Reference: http:// cn.php.net/manual/en/function.memory-get-usage.php
http://www.bkjia.com/PHPjc/324682.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324682.htmlTechArticle1. Function prototype int memory_get_usage ([ bool $real_usage = false ] ) 2. Version compatible with PHP 4 = 4.3. 2, PHP 5 3. Basic usage and examples 1, get the current memory consumption copy code...