当前位置:编程学习 > php >>

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

格式化memory_get_usage()输出

 代码如下 复制代码

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

         }

    }

}

补充:Php教程,Php基础 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,