php专区

 首页 > php专区 > PHP应用 > php函数大全 > PHP获得内存使用状态memory_get_usage()函数 - php函数

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

分享到:
【字体:
导读:
          在php中为你提供memory_get_usage函数,可以检查到你服务器当前所使用的内存情况,有了这个函数我们可以实时的检查服务器状态了,下面我来介...

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

在php中为你提供memory_get_usage函数,可以检查到你服务器当前所使用的内存情况,有了这个函数我们可以实时的检查服务器状态了,下面我来介绍memory_get_usage用法,格式化memory_get_usage()输出,代码如下:

  1. function convert($size){  
  2. $unit=array('b','kb','mb','gb','tb','pb');  
  3. return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];  
  4. }  
  5. echo convert(memory_get_usage(true));  
  6. ?> 
  7. //输出:256 kb 

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

例代码如下:

  1. if (!function_exists('memory_get_usage'))  
  2.  {  
  3.      function memory_get_usage()  
  4.      { 
  5.         $pid = getmypid(); 
  6.        if (IS_WIN)  
  7.        { 
  8.             exec('tasklist /FI "PID eq ' . $pid . '" /FO LIST'$output); 
  9.              return preg_replace('/[^0-9]/'''$output[5]) * 1024; 
  10.          }  
  11.          else 
  12.         { 
  13.            exec("ps -eo%mem,rss,pid | grep $pid"$output); 
  14.            $output = explode(" "$output[0]); 
  15.            return $output[1] * 1024; 
  16.          } 
  17.     } 
分享到:
php file_get_contents数据采集与常用见...
php file_get_contents数据采集与常用见问题解决 在批量的数据采集在php中很少会使用file_get_contents函数来操作,但是如果是小量的我们可以使用file_get_contents函数操作,因为它不但好用而且简单易学,下面我来介绍file_get_contents用法与使用过程中的问题解决办法。 先来看问题,file_get...
php html格式转文本格式代码 - php函数
php html格式转文本格式代码 在php中将html标签转换成纯文本的方法有不少,像php自带了函数strip_tags它就可以把html直接转换在纯文本文格式了,下面我来具体来看看各种转换代码。 先来看strip_tags()函数用法,下面的例子删除标记之外的所有标记,代码如下: 输入结果:This example,...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……