Home  >  Article  >  Backend Development  >  PHP gets memory usage status memory_get_usage() function_PHP tutorial

PHP gets memory usage status memory_get_usage() function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:44:551126browse

The memory_get_usage function is provided for you in php, which can check the memory currently used by your server. With this function, we can check the server status in real time. Let me introduce the usage of memory_get_usage.

Format memory_get_usage() output

Output: 256 kb
The code is as follows
 代码如下 复制代码

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

Copy code

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

 代码如下 复制代码

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;

         }

    }

}

PHP memory_get_usage() function can also have a parameter, $real_usage, whose value is a Boolean value. The default is FALSE, which means that the obtained memory usage does not include the memory occupied by this function (PHP memory manager); when set to TRUE, the obtained memory does not include the memory occupied by this function (PHP memory manager).

Example

The code is as follows Copy code
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; } } } http://www.bkjia.com/PHPjc/633079.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633079.htmlTechArticle provides you with the memory_get_usage function in php, which can check the memory currently used by your server. With this Function we can check the server status in real time, let me do it next...
Statement:
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