PHP memory_get_usage [英] PHP memory_get_usage

查看:73
本文介绍了PHP memory_get_usage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了PHP的memory_get_usage()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();

?>

哪个返回:

355120

5355216

5356008

您从中了解什么?

第一个值是在执行str_repeat()之前,因此它必须是0的值.

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

第二个是该过程之后的值,它的值可以大于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

给出:

1)5355216-355120 = 5000096字节

1) 5355216 - 355120 = 5000096 bytes

2)5356008-355120 = 5000888字节

2) 5356008 - 355120 = 5000888 bytes

如果这是它的工作方式,则我认为第一个355120字节是apache和其他模块使用的整个系统分配的内存,因为当您增加或减少<中的重复次数时,第一个值永远不会改变. c2>,只有在处理后的两个值增加或减少,但永远不会比第一个值小.

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.

推荐答案

根据php手册,memory_get_usage将为分配的内存量返回给php,不一定是所使用的内存量.

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

这篇关于PHP memory_get_usage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆