Get PHP memory consumption memory_get_usage function usage

Source: Internet
Author: User

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);
?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.