One, function prototype
int Memory_get_usage ([bool $real _usage = false])
Second, version compatible
PHP 4 >= 4.3.2, PHP 5
Iii. basic usages and examples
1, get the current memory consumption
| The code is as follows |
Copy Code |
|
<?php
Echo Memory_get_usage ();
$var = Str_repeat ("Liuhui", 10000);
Echo Memory_get_usage ();
Unset ($var);
Echo Memory_get_usage ();
?> |
Output respectively: 62328 122504 62416
Note: the Memory_get_usage () function outputs a value of bytes units
2, Format memory_get_usage () output
| The code is as follows |
Copy Code |
|
<?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 gets the size of the array or variable value
| code is as follows |
copy code |
|
<?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);
? |