Home  >  Article  >  Backend Development  >  How to obtain the amount of PHP memory cleared using the php memory_get_usage() function

How to obtain the amount of PHP memory cleared using the php memory_get_usage() function

怪我咯
怪我咯Original
2017-07-13 14:04:102653browse

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(&#39;b&#39;,&#39;kb&#39;,&#39;mb&#39;,&#39;gb&#39;,&#39;tb&#39;,&#39;pb&#39;); 
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).&#39; &#39;.$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!

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