Home > php教程 > php手册 > body text

PHP获得内存使用状态memory_get_usage()函数

WBOY
Release: 2016-06-13 09:48:03
Original
923 people have browsed it

在php中为你提供memory_get_usage函数,可以检查到你服务器当前所使用的内存情况,有了这个函数我们可以实时的检查服务器状态了,下面我来介绍memory_get_usage用法。

格式化memory_get_usage()输出

 代码如下 复制代码

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

输出:256 kb

PHP memory_get_usage() 函数还可以有个参数,$real_usage,其值为布尔值。默认为 FALSE,表示得到的内存使用量不包括该函数(PHP 内存管理器)占用的内存;当设置为 TRUE 时,得到的内存为不包括该函数(PHP 内存管理器)占用的内存。

 代码如下 复制代码

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;

         }

    }

}

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!