The PHP function memory_get_usage is used to obtain the memory consumption of PHP
- 2020-05-10 17:47:34
- OfStack
1. Function prototype
int memory_get_usage ([ bool $real_usage = false ] )
2. Version compatibility
PHP 4 > = 4.3.2, PHP 5
3. Basic usage and examples
1. Get the current memory consumption
Respectively output: 62328 122504 62416
Note: the output value of memory_get_usage() function is bytes units
2, format the output of memory_get_usage()
Output: 256 kb
3, the custom function to get the array or variable value size
Resources: http: / / cn php. net manual/en/function memory - get - usage. php
int memory_get_usage ([ bool $real_usage = false ] )
2. Version compatibility
PHP 4 > = 4.3.2, PHP 5
3. Basic usage and examples
1. Get the current memory consumption
<?php
echo memory_get_usage();
$var = str_repeat("liuhui", 10000);
echo memory_get_usage();
unset($var);
echo memory_get_usage();
?>
Respectively output: 62328 122504 62416
Note: the output value of memory_get_usage() function is bytes units
2, format the output of memory_get_usage()
<?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, the custom function to get the array or variable value size
<?php
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);
?>
Resources: http: / / cn php. net manual/en/function memory - get - usage. php