memory_get_usage [英] memory_get_usage

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

问题描述

我正在制作一些基准测试类来显示页面加载时间和内存使用情况. 加载时间已经可以使用,但是当我显示内存使用情况时,它没有变化 示例:

I'm making a little benchmark class to display page load time and memory usage. Load time is already working, but when I display the memory usage, it doesn't change Example:

$conns = array();
ob_start();
benchmark::start();
$conns[] = mysql_connect('localhost', 'root', '');
benchmark::stop();
ob_flush();

使用与

$conns = array();
ob_start();
benchmark::start();
for($i = 0; $i < 1000; $i++)
{
   $conns[] = mysql_connect('localhost', 'root', '');
}
benchmark::stop();
ob_flush();

我正在使用memory_get_usage(true)以字节为单位获取内存使用情况.

I'm using memory_get_usage(true) to get the memory usage in bytes.

推荐答案

memory_get_usage(true)将显示php引擎分配的内存量,而不是脚本实际使用的内存量.您的测试脚本很可能不需要引擎请求更多的内存.

memory_get_usage(true) will show the amount of memory allocated by the php engine, not actually used by the script. It's very possible that your test script hasn't required the engine to ask for more memory.

要进行测试,请抓住一个大的(ish)文件并将其读入内存.然后,您应该会看到一个更改.

For a test, grab a large(ish) file and read it into memory. You should see a change then.

我已经成功地使用memory_get_usage(true)来跟踪Web爬网脚本的内存使用情况,并且效果很好(因为目标是在达到系统内存限制之前放慢速度).要记住的一件事是,它不会根据实际使用情况进行更改,而是根据引擎请求的内存进行更改. 因此您最终看到的是突然的跳跃,而不是减慢了增长(或收缩)的速度.

I've successfully used memory_get_usage(true) to track the memory usage of web crawling scripts, and it's worked fine (since the goal was to slow things down before hitting the system memory limit). The one thing to remember is that it doesn't change based on actual usage, it changes based on the memory requested by the engine. So what you end up seeing is sudden jumps instead of slowing growing (or shrinking).

如果将real_usage标志设置为false,则可能会看到很小的内存更改-但是,这不能帮助您监视php请求的真实内存量系统.

If you set the real_usage flag to false, you may be able to see very small memory changes - however, this won't help you monitor the true amount of memory php is requesting from the system.

(更新:为清楚起见,我描述的区别是脚本变量使用的内存与引擎要求运行的内存之间的差异您的脚本.所有相同的脚本,不同的测量方式.)

(Update: To be clear the difference I describe is between memory used by the variables of your script, compared to the memory the engine requested to run your script. All the same script, different way of measuring.)

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

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