douzhaobo6488 2012-10-05 02:15
浏览 98
已采纳

在PHP中,如何检查某个循环使用的内存量

I'm currently making a PHP timer that checks how much time a certain way of doing something takes compared to another thing that accomplishes the same, basically benchmarking.

Now, I would also like to make this tool be able to tell how much memory that specific way of doing it is taking up.

So, for more info, I'm using microtime to check the start time, do 2000 thousand loops on that code and then do another microtime with some math to check how much time it took, so what I would like to do is, outside the microtime scope, also check for memory usage.

This is my current code:

// Set the amount of loops to do, default is 2000
$loops = 2000;
$start_time = microtime(true); // Start the timer

for($i = 0; $i < $loops; $i++) {
    // Code to test
}

$total_time = microtime(true) - $start_time; // Stop the timer, and figure out the total

ob_end_flush();   // Enable output again
echo $total_time; // Echo the timer's result
?>
  • 写回答

2条回答 默认 最新

  • duange2971 2012-10-05 02:18
    关注

    You could use memory_get_usage (http://php.net/manual/en/function.memory-get-usage.php) when you think the process is at its peak.

    Or you could also just call it occasionally and record whatever was the highest value... or as you please.

    But this is within a process. Are you talking about a PHP process "A" checking another PHP process' memory usage?

    If so:

    $myPID = getmypid();
    $stats = explode("
    ", shell_exec('pmap $(pgrep php) | grep \'total\\|\\:\''));
    for ($i = 0; $i < count($stats); $i += 2) {
        if (strpos($stats[$i], "$myPID") === false) {
            preg_match('/\d+/', $stats[$i+1], $preRes);
            $res = $preRes[0];
        }
    }
    //if $res has a value, that value is the kilobytes of memory being used by the other PHP process
    

    This solution has a problem: if you have more than 2 php processes running total, you can't guarantee that you will be getting the right process.

    To solve this, run first the other process, obtain its PID and then pass it as argument to this process. If you have the process' PID you want to check, you could just do this:

    $stats = explode("
    ", shell_exec('pmap $(pgrep php) | grep \'total\\|\\:\''));
    for ($i = 0; $i < count($stats); $i += 2) {
        if (strpos($stats[$i], "$otherPID") === 0) {
            preg_match('/\d+/', $stats[$i+1], $preRes);
            $res = $preRes[0];
        }
    }
    //$res contains the result you want in kilobytes
    

    You could check the memory for all processes that are not you, too:

    $myPID = getmypid();
    $stats = explode("
    ", shell_exec('pmap $(pgrep php) | grep \'total\\|\\:\''));
    for ($i = 0; $i < count($stats) - 1; $i += 2) {
        if (strpos($stats[$i], "$myPID") === false) {
            preg_match('/\d+/', $stats[$i+1], $preRes);
            $res[] = $preRes[0];
        }
    }
    

    So to get the maximum memory usage, just keep a $max variable and keep checking against it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置