問題描述
在我目前正在研究的系統(tǒng)中,有一個進(jìn)程將大量數(shù)據(jù)加載到數(shù)組中以進(jìn)行排序/聚合/任何操作.我知道這個過程需要優(yōu)化內(nèi)存使用,但在短期內(nèi)它只需要工作.
In a system I am currently working on, there is one process that loads large amount of data into an array for sorting/aggregating/whatever. I know this process needs optimising for memory usage, but in the short term it just needs to work.
鑒于加載到數(shù)組中的數(shù)據(jù)量,我們不斷達(dá)到內(nèi)存限制.它已經(jīng)增加了好幾倍,我想知道是否有一點增加它通常會變成一個壞主意?還是僅是機(jī)器有多少內(nèi)存的問題?
Given the amount of data loaded into the array, we keep hitting the memory limit. It has been increased several times, and I am wondering is there a point where increasing it becomes generally a bad idea? or is it only a matter of how much RAM the machine has?
機(jī)器有 2GB 的 RAM,memory_limit 目前設(shè)置為 1.5GB.我們可以輕松地為機(jī)器添加更多 RAM(無論如何都會這樣做).
The machine has 2GB of RAM and the memory_limit is currently set at 1.5GB. We can easily add more RAM to the machine (and will anyway).
其他人遇到過這種問題嗎?以及解決方案是什么?
Have others encountered this kind of issue? and what were the solutions?
推薦答案
PHP 作為 Apache 模塊運行到服務(wù)器網(wǎng)頁的 memory_limit
配置必須考慮到您可以有多少 Apache 進(jìn)程同時在機(jī)器上 - 請參閱 Apache 的 MaxClients
配置選項.
The configuration for the memory_limit
of PHP running as an Apache module to server webpages has to take into consideration how many Apache process you can have at the same time on the machine -- see the MaxClients
configuration option for Apache.
如果 MaxClients
是 100 并且您有 2,000 MB 的 RAM,一個非常快速的計算將表明您不應(yīng)使用超過 20 MB *(因為 20 MB * 100 個客戶端 = 2 GB 或 RAM,即您的服務(wù)器擁有的總內(nèi)存量)* 用于 memory_limit 值.
If MaxClients
is 100 and you have 2,000 MB of RAM, a very quick calculation will show that you should not use more than 20 MB *(because 20 MB * 100 clients = 2 GB or RAM, ie the total amount of memory your server has)* for the memory_limit value.
這還沒有考慮到可能在同一臺服務(wù)器上運行其他東西,比如 MySQL、系統(tǒng)本身,......而且 Apache 可能已經(jīng)在為自己使用一些內(nèi)存.
And this is without considering that there are probably other things running on the same server, like MySQL, the system itself, ... And that Apache is probably already using some memory for itself.
當(dāng)然,這也是一個最壞情況",即考慮到每個 PHP 頁面都在使用它可以使用的最大內(nèi)存量.
Or course, this is also a "worst case scenario", that considers that each PHP page is using the maximum amount of memory it can.
在您的情況下,如果您只需要為一項工作提供如此大的內(nèi)存,我不會為作為 Apache 模塊運行的 P?P 增加 memory_limit
.
相反,我會從命令行(或通過 cron 作業(yè)) 啟動該作業(yè),并在這種且唯一的情況下指定更高的 memory_limit
.
Instead, I would launch that job from command-line (or via a cron job), and specify a higher memory_limit
specificaly in this one and only case.
這可以通過 php 的 -d
選項來完成,例如:
This can be done with the -d
option of php, like :
$ php -d memory_limit=1GB temp.php
string(3) "1GB"
考慮到在這種情況下,temp.php 僅包含:
Considering, in this case, that temp.php only contains :
var_dump(ini_get('memory_limit'));
在我看來,這比為 Apache 增加 PHP 模塊的 memory_limit 更安全 -- 當(dāng)我有一個大型數(shù)據(jù)集或一些我無法優(yōu)化或分頁的非常重的東西時,我通常會這樣做.
In my opinion, this is way safer than increasing the memory_limit for the PHP module for Apache -- and it's what I usually do when I have a large dataset, or some really heavy stuff I cannot optimize or paginate.
如果您需要為 PHP CLI 執(zhí)行定義多個值,您還可以通過 -c
選項告訴它使用另一個配置文件,而不是默認(rèn)的 php.ini:
If you need to define several values for the PHP CLI execution, you can also tell it to use another configuration file, instead of the default php.ini, with the -c
option :
php -c /etc/phpcli.ini temp.php
那樣,你有:
/etc/php.ini
for Apache,低memory_limit
,低max_execution_time
,...- 和
/etc/phpcli.ini
用于從命令行運行的批處理,幾乎沒有限制
/etc/php.ini
for Apache, with lowmemory_limit
, lowmax_execution_time
, ...- and
/etc/phpcli.ini
for batches run from command-line, with virtually no limit
這可確保您的批處理能夠運行——并且您的網(wǎng)站仍然具有安全性(memory_limit
和 max_execution_time
是安全措施)
This ensures your batches will be able to run -- and you'll still have security for your website (memory_limit
and max_execution_time
being security measures)
盡管如此,如果您有時間優(yōu)化您的腳本,您應(yīng)該:例如,在您必須處理大量數(shù)據(jù)的情況下,分頁是必不可少的 ;-)
Still, if you have the time to optimize your script, you should ; for instance, in that kind of situation where you have to deal with lots of data, pagination is a must-have ;-)
這篇關(guān)于增加 PHP memory_limit.什么時候會變得瘋狂?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!