PHP获得内存使用状态memory_get_usage()函数
在php中为你提供memory_get_usage函数,可以检查到你服务器当前所使用的内存情况,有了这个函数我们可以实时的检查服务器状态了,下面我来介绍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 kbPHP 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获得内存使用状态memory_get_usage()函数 - php函数的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did30652