duanmajing9332 2010-10-24 22:20
浏览 36
已采纳

PHP memory_get_usage

I came across the PHP's memory_get_usage() and memory_get_peak_usage().

The problem is that I found that these two functions do not provide the real memory used by the current script. My test script is:

<?php

echo memory_get_usage();

echo '<br />';

$a = str_repeat('hello', 100000);

echo '<br />';

echo memory_get_usage();

echo '<br />';

echo memory_get_peak_usage();

?>

Which returns:

355120

5355216

5356008

What do you understand from this?

The first value is before executing the str_repeat() so it has to be the value of 0.

The second is after the process and it's OK to have a value greater than 0 but not that big value.

The third is the "peak" value and it's slightly greater than the second as I think it should be the biggest value in a processing microsecond.

So do you think that the real value of the current script's memory consumption should be like this:

memory_usage = the second memory usage - the first memory usage

peak_memory_usage = the third (peak_usage) - the first memory usage

which gives:

1) 5355216 - 355120 = 5000096 bytes

2) 5356008 - 355120 = 5000888 bytes

If this is how it works, I assume that the first 355120 bytes are the whole system allocated memory used by apache and other modules, as the first value never changes when you increase or decrease the number of repeats in the str_repeat(), only the two values after the process increase or decrease but never gets smaller that the first value.

  • 写回答

3条回答 默认 最新

  • dragon071111 2010-10-24 22:24
    关注

    According to the php manual, memory_get_usage returns the amount of memory allocated to php, not necessarily the amount being used.

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

报告相同问题?

悬赏问题

  • ¥15 手机连接电脑热点显示无ip分配
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大