跟踪PHP中的内存使用情况 [英] Tracking Memory Usage in PHP

查看:80
本文介绍了跟踪PHP中的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试跟踪处理URL的脚本的内存使用情况.基本思想是在将另一个URL添加到cURL多重处理程序之前,检查是否存在合理的缓冲区.我正在使用滚动cURL"概念,该概念在多处理程序运行时处理URL数据.这意味着,每次处理和删除现有URL时,通过从池中添加新URL可以保持N个连接处于活动状态.

I'm trying to track the memory usage of a script that processes URLs. The basic idea is to check that there's a reasonable buffer before adding another URL to a cURL multi handler. I'm using a 'rolling cURL' concept that processes a URLs data as the multi handler is running. This means I can keep N connections active by adding a new URL from a pool each time an existing URL processes and is removed.

我用memory_get_usage()取得了一些积极的结果.添加real_usage标志很有帮助(虽然并不清楚系统"内存和"emalloc"内存之间的区别,但是系统显示的数字更大).当添加URL时,memory_get_usage()确实会增加,而随着URL集的耗尽,memory_get_usage()会降低.但是,我刚刚超过了32M限制,最后一次内存检查为〜18M.

I've used memory_get_usage() with some positive results. Adding the real_usage flag helped (not really clear on the difference between 'system' memory and 'emalloc' memory, but system shows larger numbers). memory_get_usage() does ramp up as URLs are added then down as the URL set is depleted. However, I just exceeded the 32M limit with my last memory check being ~18M.

每当cURL multi发出请求返回信号时,我都会查询内存使用情况.由于可能同时返回多个请求,因此一堆URL可能同时返回数据,并且实际上使内存使用量跃升了14M.但是,如果memory_get_usage()是准确的,我想就是这样.

I poll the memory usage each time cURL multi signals a request has returned. Since multiple requests may return at the same time, there's a chance a bunch of URLs returned data at the same time and actually jumped the memory usage that 14M. However, if memory_get_usage() is accurate, I guess that's what's happening.

[更新:在问我猜之前,应该运行更多测试,增加php的内存限制(但在脚本中保留安全"数量相同),并且报告的内存使用量确实从低于我自己设定的2500万到3200万以上的上限.然后,如预期的那样,缓慢地降低为未添加URL的位置.但我会提一个问题:这是正确的方法吗?]

[Update: Should have run more tests before asking I guess, increased php's memory limit (but left the 'safe' amount the same in the script) and the memory usage as reported did jump from below my self imposed limit of 25M to over 32M. Then, as expected slowly ramped down as URLs where not added. But I'll leave the question up: Is this the right way to do this?]

我可以这样信任memory_get_usage()吗?是否有更好的替代方法来获取内存使用情况(我已经看过一些脚本来解析shell命令的输出)?

Can I trust memory_get_usage() in this way? Are there better alternative methods for getting memory usage (I've seen some scripts parse the output of shell commands)?

推荐答案

real_usage如此运行:

Zend的内存管理器并未针对其所需的每个块使用系统malloc.相反,它分配了很大的系统内存块(以256K为增量,可以通过设置环境变量ZEND_MM_SEG_SIZE进行更改)并在内部进行管理.因此,有两种内存使用情况:

Zend's memory manager does not use system malloc for every block it needs. Instead, it allocates a big block of system memory (in increments of 256K, can be changed by setting environment variable ZEND_MM_SEG_SIZE) and manages it internally. So, there are two kinds of memory usage:

  1. 引擎从操作系统占用了多少内存(实际使用情况")
  2. 应用程序实际使用了多少内存(内部使用情况")

其中任何一个都可以由memory_get_usage()返回.哪一种对您更有用取决于您要寻找的内容.如果您要优化特定部分中的代码,则内部"对您可能更有用.如果您要全局跟踪内存使用情况,则真实"将更有用. memory_limit限制实数",因此,一旦限制所允许的所有块都从系统中取出,并且内存管理器无法分配请求的块,分配就会失败.请注意,在这种情况下,内部"使用量可能小于限制,但是由于碎片的原因,分配仍然可能失败.

Either one of these can be returned by memory_get_usage(). Which one is more useful for you depends on what you are looking into. If you're looking into optimizing your code in specific parts, "internal" might be more useful for you. If you're tracking memory usage globally, "real" would be of more use. memory_limit limits the "real" number, so as soon as all blocks that are permitted by the limit are taken from the system and the memory manager can't allocate a requested block, there the allocation fails. Note that "internal" usage in this case might be less than the limit, but the allocation still could fail because of fragmentation.

此外,如果您使用某些外部存储器跟踪工具,则可以设置 环境变量USE_ZEND_ALLOC=0会禁用上述机制,并使引擎始终使用malloc().这会带来更差的性能,但允许您使用malloc跟踪工具.

Also, if you are using some external memory tracking tool, you can set this environment variable USE_ZEND_ALLOC=0 which would disable the above mechanism and make the engine always use malloc(). This would have much worse performance but allows you to use malloc-tracking tools.

另请参见有关此内存管理器的文章,它也有一些代码示例.

See also an article about this memory manager, it has some code examples too.

这篇关于跟踪PHP中的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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