PHP obtains the memory usage status memory_get_usage () function _ PHP Tutorial

Source: Internet
Author: User
PHP obtains the memory usage status memory_get_usage () function. The memory_get_usage function is provided for you in php to check the current memory usage on your server. with this function, we can check the server status in real time, next I will provide you with the memory_get_usage function in php, which can check the current memory usage on your server. with this function, we can check the server status in real time, next I will introduce the usage of memory_get_usage.

Format 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

The PHP memory_get_usage () function can also have a parameter $ real_usage with a Boolean value. The default value is FALSE, indicating that the obtained memory usage does not include the memory occupied by this function (PHP memory manager). if it is set to TRUE, the resulting memory does not include the memory occupied by this function (PHP memory manager.

Example

The code is as follows:

If (! Function_exists ('memory _ get_usage '))

{
Function memory_get_usage ()

{

$ Pid = getmypid ();

If (IS_WIN)

{

Exec ('tasklist/FI "PID eq '. $ pid.'"/FO list', $ output );

Return preg_replace ('/[^ 0-9]/', ', $ output [5]) * 1024;

}

Else

{

Exec ("ps-eo % mem, rss, pid | grep $ pid", $ output );

$ Output = explode ("", $ output [0]);

Return $ output [1] * 1024;

}

}

}

...

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.