即使未达到 memory_limit,PHP 内存不足错误
我刚刚继承了一个带有 PHP 脚本的站点,该脚本始终以 117 MB 的速度耗尽内存.即使我通过 php.ini 将 PHP 的 memory_limit 变量增加到 312 MB,也会发生这种情况.
I have just inherited a site with a PHP script that is consistently running out of memory at 117 MB. This happens even when I increase PHP's memory_limit variable to 312 MB, which I'm doing via php.ini.
多亏了 pcguru 的一个很好的线索,这个问题现在已经解决了.请参阅下面开始的答案:我终于找到了答案
ini_get('memory_limit')
返回在 php.ini 中设置的值,因此我确定更改该值后 Apache 已重新启动.我正在使用 memory_get_usage(true)
返回脚本在整个过程中的各个点消耗的内存.当它达到 117 MB 时,它一直失败.
ini_get('memory_limit')
returns the value set in php.ini, so I'm certain Apache has restarted after changing the value. I'm using memory_get_usage(true)
to return the memory consumed by the script at various points along the way. And it's consistently failing when it gets to 117 MB.
是否有一些我不知道的内部 PHP 限制,它从未为单个脚本分配超过 117MB 的空间?
Is there some internal PHP limit I'm unaware of that has it never allocate more than 117MB to an individual script?
服务器有 1GB 的内存并运行 CentOS.我有 root shell 访问权限.PHP 是 5.3.18 版本.MySQL 是 5.1.66-cll 版本.
The server has 1GB of RAM and is running CentOS. I have root shell access. PHP is version 5.3.18. MySQL is version 5.1.66-cll.
此脚本位于用户名/密码后面,我无法提供对其的公开访问.
This script is behind a username/password and I can't provide public access to it.
编辑添加:
1) 感谢大家迄今为止的帮助.您可以在我对特定用户评论的回复中找到更多信息,并在下面的各种答案下找到.
1) Thanks all for your help to date. You'll find more info in my replies to specific user comments under various answers below.
2) Suhosin 肯定没有安装.我已经检查了多个地方,包括运行脚本和检查常量以及运行 php -v
2) Suhosin is definitely not installed. I've checked in multiple places including running a script and check for constants and running php -v
3) apache 日志没有记录我收到的特定错误消息.日志记录在 php.ini 中打开.我通过 grep 来搜索整个日志.
3) The apache log has no record of the specific error message I'm getting. Logging is switched on in php.ini. I piped through grep to search the entire log.
4) 是否有可能在这种情况下报告了错误的错误?
4) Is it possible the wrong error is being reported in this case?
推荐答案
我终于找到了答案.线索来自 pcguru 的回答,开头是因为服务器只有 1 GB 的 RAM...".
I have finally found the answer. The clue came from pcguru's answer beginning 'Since the server has only 1 GB of RAM...'.
我凭直觉查看了 Apache 是否有自己的内存限制,因为这些限制可能会影响 PHP 分配内存的能力.就在 httpd.conf 的顶部,我发现了以下声明:RLimitMEM 204535125
On a hunch I looked to see whether Apache had memory limits of its own as those were likely to affect PHP's ability to allocate memory. Right at the top of httpd.conf I found this statement: RLimitMEM 204535125
这是由 whm/cpanel 放在那里的.根据以下网页 whm/cpanel 在虚拟服务器上错误地计算了其值...http://forums.jaguarpc.com/vps-dedicated/17341-apache-memory-limit-rlimitmem.html
This is put there by whm/cpanel. According to the following webpage whm/cpanel incorrectly calculates its value on a virtual server... http://forums.jaguarpc.com/vps-dedicated/17341-apache-memory-limit-rlimitmem.html
内存不足的脚本大部分通过了,所以我将 RLimitMEM 增加到 268435456 (256 MB) 并重新运行脚本.它完成了数组合并并生成了 csv 文件以供下载.
The script that runs out of memory gets most of the way through, so I increased RLimitMEM to 268435456 (256 MB) and reran the script. It completed its array merge and produced the csv file for download.
ETA:在进一步阅读 RLimitMEM 和 RLimitCPU 之后,我决定将它们从 httpd.conf 中删除.这允许 ini_set('memory_limit','###M') 工作,我现在为该特定脚本提供它需要的额外内存.我还将那台服务器上的 RAM 增加了一倍.
ETA: After further reading about RLimitMEM and RLimitCPU I decided to remove them from httpd.conf. This allows ini_set('memory_limit','###M') to work, and I now give that particular script the extra memory it needs. I also doubled the RAM on that server.
感谢大家帮助检测这个相当棘手的问题,尤其是 pcguru,他提出了让我找到解决方案的重要线索.
Thank you to everyone for your help in detecting this rather thorny issue, and especially to pcguru who came up with the vital clue that got me to the solution.
相关文章