增加PHP memory_limit.在什么时候变得疯狂? [英] Increasing PHP memory_limit. At what point does it become insane?

查看:79
本文介绍了增加PHP memory_limit.在什么时候变得疯狂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前正在使用的系统中,有一个过程将大量数据加载到数组中以进行排序/聚合/处理.我知道此过程需要针对内存使用进行优化,但是从短期来看,它只是需要工作.

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.

鉴于加载到数组中的数据量,我们不断达到内存限制.它已经增加了好几次,我想知道在某种程度上增加它通常是个坏主意吗?还是与计算机有多少RAM有关?

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?

机器具有2GB的RAM,当前的memory_limit设置为1.5GB.我们可以轻松地向计算机添加更多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?

推荐答案

在服务器网页上作为Apache模块运行的PHP memory_limit的配置必须考虑到您可以同时具有多少个Apache进程在计算机上-请参阅MaxClients Apache的配置选项.

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,那么非常快速的计算将显示您不应使用超过20 MB *(因为20 MB * 100个客户端= 2 GB或RAM,即总数量服务器的内存空间)*作为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.

这并没有考虑到同一台服务器上可能还有其他东西在运行,例如MySQL,系统本身……而Apache可能已经在使用一些内存了.

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.

或者,当然,这也是一种最坏的情况",它认为每个PHP页面都使用了它可以使用的最大内存量.

Or course, this is also a "worst case scenario", that considers that each PHP page is using the maximum amount of memory it can.


在您的情况下,如果仅一项工作就需要如此大的内存,那么我不会增加作为Apache模块运行的PḦP的memory_limit.


In your case, if you need such a big amount of memory for only one job, I would not increase the memory_limit for PḦP running as an Apache module.

相反,我将从命令行(或通过cron作业)启动该作业,并在这种唯一的情况下具体指定一个更高的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安全得多-这是我在拥有大型数据集或某些我无法优化或分页的非常繁重的工作时通常会执行的操作.

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执行定义多个值,还可以通过-c选项告诉它使用另一个配置文件,而不是默认的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适用于Apache,memory_limit低,max_execution_time低,...
  • /etc/phpcli.ini用于从命令行运行的批处理,几乎没有限制
  • /etc/php.ini for Apache, with low memory_limit, low max_execution_time, ...
  • and /etc/phpcli.ini for batches run from command-line, with virtually no limit

这可确保您的批次能够运行-并且您仍将拥有网站的安全性(memory_limitmax_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)


不过,如果您有时间优化脚本,则应该;例如,在这种情况下,您必须处理大量数据,分页是必不可少的;-)


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 ;-)

这篇关于增加PHP memory_limit.在什么时候变得疯狂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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