使用 apache 的另一个 PHP 内存不足问题
我知道这是一个老问题!但这一次我的情况有所不同。
我在 OpenSuse 上运行 php/mysql/apache。我已经将php设置为128MB内存。我没有任何适用于 apache 的 RLimitMEM
。
一切都很好,鸟儿在歌唱,鲜花盛开,但一旦我重新启动我的阿帕奇,糟糕的事情就发生了!
我收到 PHP Fatal error: out of memory blah blah
。所有运行良好的 php 驱动的应用程序现在都记录此错误。发生了什么?我尝试了memory_limit = 512M,但没有任何进展。
我很困惑为什么我所有的 php 文件现在都会抛出这个错误,除了非常小的脚本。我尝试了 phpinfo(),确实有 128MB 内存。 (我知道 php apache ini 设置与独立 php 不同,我配置了正确的设置!)。
更新:
确切的 php 错误示例:
[Mon Jul 25 04:22:25 2011] [error] [client 31.57.122.252] PHP 致命错误:内存不足(已分配 1048576)(已尝试)分配 491520 字节) /srv/www/htdocs/phpMyAdmin/libraries/common.inc.php 第 796 行
[Mon Jul 25 04:26:39 2011] [error] [client 31.57.122.252] PHP 致命错误:内存不足(已分配 1048576)(尝试分配 4 个字节) /srv/sbuta/library/Zend/Config/Ini.php 第 172 行
[Mon Jul 25 04:27:02 2011] [error] [client 31.57.122.252] PHP 致命错误:超出内存(已分配 1048576)(尝试分配 30720 字节) /srv/kode/library/Zend/Config/Ini.php 第 132 行
I know it is an old question! But this time I have different situation.
I'm running php/mysql/apache on OpenSuse. I have set php with 128MB of memory. I don't have any RLimitMEM
for apache.
Everything was fine, birds singing, flowers blossoming but once I restarted my apache then bad things happened!
I get PHP Fatal error: out of memory blah blah
. All php powered applications that were working fine, now log this error. What happened? I tried memory_limit = 512M without any progress.
I'm confused why all my php files now throw this error except really small scripts. I tried phpinfo() and I really have 128MB of memory there. ( And I know that php apache ini setting is different from standalone php, I configured the correct one! ).
UPDATE:
exact php error samples:
[Mon Jul 25 04:22:25 2011] [error] [client 31.57.122.252] PHP Fatal error: Out of memory (allocated 1048576) (tried to allocate 491520 bytes) in /srv/www/htdocs/phpMyAdmin/libraries/common.inc.php on line 796
[Mon Jul 25 04:26:39 2011] [error] [client 31.57.122.252] PHP Fatal error: Out of memory (allocated 1048576) (tried to allocate 4 bytes) in /srv/sbuta/library/Zend/Config/Ini.php on line 172
[Mon Jul 25 04:27:02 2011] [error] [client 31.57.122.252] PHP Fatal error: Out of memory (allocated 1048576) (tried to allocate 30720 bytes) in /srv/kode/library/Zend/Config/Ini.php on line 132
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是没有剩余物理内存了!禁用某些 php 或 apache 模块应该可以解决该问题。
分步指南(:D) :
/etc/php5/apache2/php.ini
搜索memory_limit = ...
。 (要测试它,请尝试在服务器中放入一个具有一些奇怪名称的文件,其中包含以下内容:并查看那里的 memory_limit 值)。
/etc/security/limits.conf
中的 wwwrun(或 apache 运行的用户)是否有任何安全限制记住在进行任何更改后重新启动 apache 以对其进行测试。
The problem is that there is no remaining physical memory! disabling some php or apache modules should fix the problem.
Step by step guide(:D) :
/etc/php5/apache2/php.ini
search formemory_limit = ...
. ( to test it, try putting a file with some strange name in your server with this content:<?php echo phpinfo();?>
and see memory_limit value there ).cd /etc/apache2/ && grep -irn 'RLimitMEM'
to find out./etc/security/limits.conf
Remember to restart apache after any change to test it.
内存不足错误是由于脚本在内存中存储了过多数据而导致的。如果您有一个包含大量数据集的数据库并使用 fetchAll() 提取数据而不是过滤,您可能会很快耗尽内存。另一个问题是存储如此大数据集的对象。最后但并非最不重要的一点是,您可能有一个循环将数据添加到内存中,直到达到限制。
检查脚本中是否有提取数据并使用 XDebug 的位置。它可以显示内存使用量的增加。
更新
诡异的。您在这里遇到的“内存不足”错误不是您通常在 php memory_limit 配置中遇到的错误(即“内存耗尽”),并且由于您在所有应用程序中都遇到了该错误,因此更加清楚。
服务器环境似乎存在限制,在您的情况下是 Apache。也许您实际上可以/必须在conf 文件中使用 RLimitMEM 设置
max
限制。默认情况下使用操作系统默认值,即 OpenSuse。如果您突然对运行您的 Apache 模块的用户产生了限制,这可能会影响到您的 Apache 环境,然后一直影响到您的 PHP 脚本,最终会出现问题。第二次更新
是时候隔离问题了。因为它是在您重新启动服务器时开始的,并且您的所有应用程序都遇到此问题,所以它一定是您的服务器环境中的某些问题。尝试使用默认的 Apache 配置启动并运行 phpMyAdmin。如果这已经导致错误,您必须查看您的 Linux 服务器。
Out of memory errors are the result of too much data stored in memory by your script. If you have a database with large data sets and pull data with fetchAll() instead of filtering you may run out of memory real quick. Another problem are objects where you store such large data sets. Last but not least you may have a loop that adds data to memory until it reaches the limit.
Check you scripts for places where you pull data and use XDebug. It can show you the increase on memory usage.
Update
Weird. The "out of memory" error you have here is not the error you usual get with the php memory_limit config (that has "memory exhausted") and since you have it with all apps this is even more clear.
There seems to be a limit imposed from the server environment which in your case is Apache. Maybe you can/must in fact set a
max
limit with RLimitMEM in your conf file. The default is to use the operating systems default which is your OpenSuse. If you suddenly have limitations for the user running your Apache module this may go to your Apache environment and then all the way to your PHP scripts which is finally complaining.2nd Update
Time to isolate the problem. Because it began when you restarted your server and all your apps are experiencing this problem it has to be something in your server environment. Try to start and run phpMyAdmin with a default Apache config. If that already causes the error you have to look at your linux server.
PHP 每次重新加载时都会重新启动其脚本;除非您的脚本全天候(24/7)运行,否则它不会受到 Apache 持续正常运行时间的影响。当 Apache 的内存使用量增加时,它要么有更多的 PHP 进程,要么像 Apache 和 PHP 一样开始泄漏内存。
假设您有一个包含 100000000 个包含“foo”元素的数组,并将它们传递给另一个函数,该函数调用将复制该数组,从而有效地使 PHP 内存使用量增加一倍。通过很少收到这样的调用,您很容易出现脚本内存不足的情况。
PHP restarts its scripts for each reload; unless you script runs 24/7, it doesn’t get affected by continue uptime of Apache. When Apache’s memory usage grows, it either has more PHP processes or it just started to leak memory like normal with Apache and PHP.
Given you have an array that has 100000000 elements containing "foo" and you pass them to another function, that function call copies that array, effectively doubling PHP memory usage. By getting few calls like this, you can easily get a situation where your script is out of memory.
我的一台服务器上遇到了这个问题,结果是 Gnome3 桌面环境的问题。在内存为 2 Gig 或更少的 Debian 和 Ubuntu 服务器上,如果安装了 Gnome3 或 Unity 桌面环境,它会消耗大量桌面内存,以至于开始耗尽 apache 所需的 RAM,从而导致内存不足错误。
通过安装“Gnome-Classic”、Mate 或 LXDE 桌面环境并登录来解决该问题。
I had this issue on one of my servers and it turned out to be a problem with the Gnome3 desktop environment. On Debian and Ubuntu servers with 2 Gigs of memory or less, if you have the Gnome3 or Unity desktop environment installed, it eats up so much desktop memory that it starts using up RAM that apache needs which leads to the out of memory error.
Resolve the issue by installing either the "Gnome-Classic", Mate or LXDE desktop environment and logging into that instead.