Home > Article > Backend Development > How to obtain the amount of PHP memory cleared using the php memory_get_usage() function
memory_get_usage --Returns the amount of memory currently allocated to your PHP script, in bytes.
int memory_get_usage ([ bool $real_usage = false ] )
real_usage
If set to TRUE, get the total memory size allocated by the system, including unused pages. If not set or set to FALSE, only the actual amount of memory used is reported.
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
Description: memory_get_usage() function output The value is in bytes unit
2, formatted memory_get_usage() output
<?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 functionGet 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); ?>
The above is the detailed content of How to obtain the amount of PHP memory cleared using the php memory_get_usage() function. For more information, please follow other related articles on the PHP Chinese website!