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

怪我咯
Release: 2023-03-13 20:20:02
Original
2675 people have browsed it

memory_get_usage --Returns the amount of memory currently allocated to your PHP script, in bytes.

int memory_get_usage ([ bool $real_usage = false ] )
Copy after login


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

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)); 
?>
Copy after login

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); 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template