PHP memory_get_usage () function How to get PHP memory consumption

Source: Internet
Author: User
Memory_get_usage--Returns the amount of memory currently assigned to your PHP script, in bytes (byte).

int Memory_get_usage ([bool $real _usage = false])


Real_usage

If set to TRUE, gets the total memory size of the system allocation, including unused pages. If not set or set to FALSE, only the amount of memory actually used is reported.

Basic usage and examples
1, gets the current memory consumption

<?php echo Memory_get_usage (); $var = Str_repeat ("Liuhui", 10000); Echo Memory_get_usage (); Unset ($var); Echo Memory_get_usage ();?>

Output separately: 62328 122504 62416
Description: The value of the Memory_get_usage () function output is bytes units

2, Format memory_get_usage () output

<?php function Convert ($size) {$unit =array (' B ', ' KB ', ' MB ', ' GB ', ' TB ', ' PB '), return @round ($size/pow (1024x768, ($i =floor (Log ($size, 1024)))), 2). ' '. $unit [$i]; } Echo Convert (Memory_get_usage (true));?>

Output: KB

3, Custom function get array or variable value size

<?php function Array_size ($arr) {Ob_start (); Print_r ($arr); $mem = Ob_get_contents (); Ob_end_clean (); $mem = Preg_repl Ace ("/\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.