PHP内存分配限制原因

时间:2014-10-29 17:29:08

标签: php linux memory out-of-memory dev-to-production

我有2台服务器

两台服务器都有相同的128位数据的php memory_limit。

我的开发服务器运行一个脚本就好了,而在我的prod服务器上,我收到一个致命的错误:允许的内存大小为134217728字节耗尽(试图分配32个字节)...

我的问题是,即使我的php memory_limits是相同的,我在prod环境中会耗尽内存的其他原因是什么?

1 个答案:

答案 0 :(得分:1)

<强>前言

PHP是运行Apache [HTTPD Server]顶部的模块,这涉及将php解释器与webserver发布的钩子库链接

<强>原因

Now it can exhaust due to scripts running allocating memory [RAM] & reach its threshold & get such errors.

Example big loops running & saving lots of data in memory which may over RUN the Memory

您可以做的可能优化

memory_limit = 32M to your server's main php.ini file (recommended, if you have access)
php_value memory_limit 32M in your .htaccess file in the 

这些是你跑出内存的页面的一些解决方法

ini_set('memory_limit', '-1'); overrides the default PHP memory limit (On individual php pages wherever you need extra memory)

您还可以在HTTP Server上进行一些优化(apache.conf或http.conf)

RLimitCPU,RLimitNPROC, RLimitMEM parameters can be adusted

由于您的内存不足,您可以调整RLimitMEM

Syntax: RLimitMEM soft-bytes [hard-bytes]
Example: RLimitMEM 1048576 2097152

This directive sets the soft and hard limits for maximum memory usage of a process in bytes. It takes one or two parameters. The first parameter sets the soft resource limit for all processes. The second parameter sets the maximum resource limit. Either parameter can be a number, or ``max'', which indicates to the server that the limit should match the maximum allowed by the operating system configuration. Raising the maximum resource limit requires the server to be running as the user ``root'' or in the initial start-up phase.

你也可以将这些行放在.htaccess文件中[因为在共享主机中你没有访问php.ini&amp; http.conf文件