Use the PHP function memory_get_usage to get the PHP memory consumption. 1. function prototype intmemory_get_usage ([bool $ real_usagefalse]) 2. compatible with PHP44.3.2 and PHP5 3. basic usage and instance 1, get the current memory consumption replica generation
I. function prototype
Int memory_get_usage ([bool $ real_usage = false])
2. version compatibility
PHP 4> = 4.3.2, PHP 5
III. basic usage and examples
1. get the current memory consumption
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: 62328 122504 62416
Note: The value output by the memory_get_usage () function is bytes.
2. format the memory_get_usage () output.
The code is as follows:
Function convert ($ size ){
$ Unit = array ('B', 'KB', 'mb', 'GB', 'TB', 'petab ');
Return @ round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2 ). ''. $ unit [$ I];
}
Echo convert (memory_get_usage (true ));
?>
Output: 256 kb
3. the user-defined function obtains the array or variable value size.
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 );
?>
References: http://cn.php.net/manual/en/function.memory-get-usage.php
Extends int memory_get_usage ([bool $ real_usage = false]) 2. compatible with PHP 4 = 4.3.2 and PHP 5 3. basic usage and instance 1, get the current memory consumption replica generation...