Home  >  Article  >  Backend Development  >  childhoodmemory uses the PHP function memory_get_usage to obtain the amount of PHP memory cleared

childhoodmemory uses the PHP function memory_get_usage to obtain the amount of PHP memory cleared

WBOY
WBOYOriginal
2016-07-29 08:47:24884browse

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();
?>


Output respectively: 62328 122504 62416
Explanation: The value output by the memory_get_usage() function is in bytes unit
2, formatted memory_get_usage() output

Copy code The code is as follows :


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 the 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

The above introduces the childmemory method of using the PHP function memory_get_usage to obtain PHP memory clearing consumption, including the content of childmemory. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn