問題描述
我正在嘗試跟蹤處理 URL 的腳本的內(nèi)存使用情況.基本思想是在將另一個(gè) URL 添加到 cURL 多處理程序之前檢查是否存在合理的緩沖區(qū).我正在使用滾動(dòng) cURL"概念,該概念在多處理程序運(yùn)行時(shí)處理 URL 數(shù)據(jù).這意味著每次現(xiàn)有 URL 處理和刪除時(shí),我都可以通過從池中添加一個(gè)新 URL 來保持 N 個(gè)連接處于活動(dòng)狀態(tài).
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()
并獲得了一些積極的結(jié)果.添加 real_usage
標(biāo)志有幫助(不太清楚系統(tǒng)"內(nèi)存和emalloc"內(nèi)存之間的區(qū)別,但系統(tǒng)顯示更大的數(shù)字).memory_get_usage()
確實(shí)會(huì)隨著 URL 的添加而上升,然后隨著 URL 集的耗盡而下降.然而,我剛剛超過了 32M 的限制,我上次的內(nèi)存檢查是 ~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 表示請(qǐng)求返回時(shí),我都會(huì)輪詢內(nèi)存使用情況.由于多個(gè)請(qǐng)求可能同時(shí)返回,因此有可能一堆URL同時(shí)返回?cái)?shù)據(jù)并實(shí)際跳過14M的內(nèi)存使用.但是,如果 memory_get_usage()
是準(zhǔn)確的,我想這就是正在發(fā)生的事情.
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.
[更新:在詢問我猜之前應(yīng)該運(yùn)行更多測(cè)試,增加了 php 的內(nèi)存限制(但在腳本中保留了相同的安全"數(shù)量)并且報(bào)告的內(nèi)存使用量確實(shí)從低于我自己施加的 25M 到超過 32M 的限制.然后,正如預(yù)期的那樣,隨著未添加的 URL 逐漸減少.但我會(huì)留下這個(gè)問題:這是正確的方法嗎?]
[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()
嗎?是否有更好的替代方法來獲取內(nèi)存使用情況(我見過一些腳本解析 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 的內(nèi)存管理器不會(huì)為它需要的每個(gè)塊使用系統(tǒng) malloc.相反,它會(huì)分配一大塊系統(tǒng)內(nèi)存(以 256K 為增量,可以通過設(shè)置環(huán)境變量 ZEND_MM_SEG_SIZE
來更改)并在內(nèi)部對(duì)其進(jìn)行管理.所以,有兩種內(nèi)存使用:
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:
- 引擎從操作系統(tǒng)中占用了多少內(nèi)存(實(shí)際使用情況")
- 應(yīng)用實(shí)際使用了多少內(nèi)存(內(nèi)部使用")
其中之一可以由 memory_get_usage()
返回.哪一個(gè)對(duì)您更有用取決于您正在研究什么.如果您正在考慮優(yōu)化特定部分的代碼,內(nèi)部"可能對(duì)您更有用.如果您要全局跟蹤內(nèi)存使用情況,真實(shí)"會(huì)更有用.memory_limit
限制了真實(shí)"數(shù)量,因此只要限制允許的所有塊都從系統(tǒng)中取出并且內(nèi)存管理器無法分配請(qǐng)求的塊,分配就會(huì)失敗.請(qǐng)注意,這種情況下的內(nèi)部"使用量可能小于限制,但由于碎片化,分配仍然可能失敗.
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.
另外,如果你正在使用一些外部內(nèi)存跟蹤工具,你可以設(shè)置這個(gè)環(huán)境變量 USE_ZEND_ALLOC=0
將禁用上述機(jī)制并使引擎始終使用 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.
另請(qǐng)參閱一篇關(guān)于此內(nèi)存管理器的文章,它也有一些代碼示例.
See also an article about this memory manager, it has some code examples too.
這篇關(guān)于在 PHP 中跟蹤內(nèi)存使用情況的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!