Use the PHP function memory_get_usage to get the PHP memory consumption.

Source: Internet
Author: User
The PHP memory consumption needs to be obtained during the performance optimization process. The memory_get_usage () function can be used to obtain the current memory consumption. The function is easy to use. Here we will discuss the usage and instance of the memory_get_usage () function.

The PHP memory consumption needs to be obtained during the performance optimization process. The memory_get_usage () function can be used to obtain the current memory consumption. The function is easy to use. Here we will discuss the usage and instance of the memory_get_usage () function.

I. function prototype
Int memory_get_usage ([bool $ real_usage = false])

2. Version compatibility
PHP 4> = 4.3.2, PHP 5

III. Basic usage and Examples
1. Get the current memory consumption
The Code is as follows:
Echo memory_get_usage ();
$ Var = str_repeat ("liuhui", 10000 );
Echo memory_get_usage ();
Unset ($ var );
Echo memory_get_usage ();
?>

Output: 62328 122504 62416
Note: The value output by the memory_get_usage () function is bytes.

2. format the memory_get_usage () output.
The Code is as follows:
Function convert ($ size ){
$ Unit = array ('B', 'kb', 'mb', 'gb', 'tb', 'petab ');
Return @ round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2 ). ''. $ unit [$ I];
}
Echo convert (memory_get_usage (true ));
?>

Output: 256 kb

3. the user-defined function obtains the array or variable value size.
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 );
?>

References:

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.