Home > Backend Development > PHP Tutorial > How to use the PHP function memory_get_usage to obtain the amount of PHP memory clearing_PHP tutorial

How to use the PHP function memory_get_usage to obtain the amount of PHP memory clearing_PHP tutorial

WBOY
Release: 2016-07-21 15:22:34
Original
971 people have browsed it

1. Function prototype
int memory_get_usage ([ bool $real_usage = false ] )

2. Version compatible
PHP 4 >= 4.3. 2, PHP 5

3. Basic usage and examples
1. Get the current memory consumption

Copy code The code is as follows:

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: The value output by the memory_get_usage() function For bytes unit

2, format memory_get_usage() output
Copy the code The code is as follows:

< ;?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 function to get the array or variable value size
Copy code 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);
?>

Reference: http:// cn.php.net/manual/en/function.memory-get-usage.php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324682.htmlTechArticle1. Function prototype int memory_get_usage ([ bool $real_usage = false ] ) 2. Version compatible with PHP 4 = 4.3. 2, PHP 5 3. Basic usage and examples 1, get the current memory consumption copy code...
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