PHP内存分配限制原因

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

我有2台服务器

两个服务器都具有相同的 128M 数据的 php memory_limit。

我的开发服务器运行脚本很好,而在我的生产服务器上我收到一个 fatal error :134217728 字节的允许内存大小已耗尽(试图分配 32 字节)...

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

最佳答案

前言

PHP 是运行在 Apache [HTTPD 服务器] 之上的模块,这涉及将 php 解释器链接到网络服务器发布的 Hook 库

原因

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 服务器(apache.conf 或 http.conf)上进行一些优化

RLimitCPU、RLimitNPROC、RLimitMEM 参数可调整

由于内存不足,您可以调整 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 和 http.conf 文件

关于PHP内存分配限制原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26636980/

相关文章:

php - mysqli_fetch_array 数据放入数组并存储在 php 变量中

c++ - 从启动时保留的内存写入时,内核 AIO (libaio) 写入请求失败

linux - Linux 中使用 Nohup like 的多程序调用

c - 自然对齐的内存地址

linux - 我该如何修复这个解析 linux smap 的 perl 脚本?

memory - cpu 如何决定将哪些数据放入哪些内存(ram、缓存、寄存器)?

php - Laravel 5.4 中的快速身份验证和配置

php - Twitter OAUTH 返回一个空数组

php - MySQL 只能插入 10,000 行

c - 如何使用多个写入器和多个读取器来实现 Readers Writers