PHP 致命错误:允许的内存大小为 268435456 字节已用尽(试图分配 XX 字节)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20486365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:14:21  来源:igfitidea点击:

PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate XX bytes)

phpmemory-limit

提问by waki

Initially, it was stated in the settings of 128MB, then I got this error, I thought that maybe did not have enough memory, then I increased to 256MB, but the error continues.

一开始是在128MB的设置里说的,后来出现这个错误,我以为可能是内存不够,后来我增加到256MB,但是错误依旧。

String in code with this error:

出现此错误的代码中的字符串:

function clean($str) {
$clean_str = stripslashes (trim($str));
return $clean_str;
}

// clean slashes
foreach($result[$i] as $key=>$value) {
 if($key=="currency") continue;
 $result[$i][$key] = clean($result[$i][$key]);
}

Why is this happening ?

为什么会这样?

回答by zeantsoi

Modify your php.inito increase your memory_limitto something higher than what you have currently provisioned – 512MB is not unusual for modern day applications.

修改您的值php.ini以将其增加到memory_limit高于您当前配置的值 – 512MB 对于现代应用程序来说并不罕见。

回答by Kzqai

256MB (the default these days, and the amount that 268435456 bytes amounts to), is a lot of memory for a script, so if you're exceeding it, the first things to check for are a few scenarios:

256MB(这些天的默认值,以及 268435456 字节的总和)对于脚本来说是很大的内存,所以如果你超过了它,首先要检查的是几个场景:

An infinite loop will exhaust the memory limit:

无限循环将耗尽内存限制:

var $storage = null;
while(true){
  $storage += 'infinity!'; // Or something even more resource requiring.
}

Alternatively, if you are pulling data from a database and accidentally pull too much from a table with a lot of data, and no limit in the sql statement, that can exhaust your php script memory:

或者,如果您从数据库中提取数据,并且不小心从包含大量数据的表中提取了太多数据,并且 sql 语句中没有限制,则可能会耗尽您的 php 脚本内存:

select * from users where true; // On a million-row table, this could do it.

So in general, this message is about the script exhausting it's memory, but it's usually not a call to raise the limit so much as a call to figure out why your script is misbehaving.

所以总的来说,这条消息是关于脚本耗尽内存的,但通常不是提高限制的呼吁,而是找出脚本行为不当的原因。

回答by Eric

I was getting these errors all of a sudden about a day and a half ago in my error logs when trying to activate plugins. Which was causing blank/white screens.

大约一天半前,当我尝试激活插件时,我的错误日志中突然出现了这些错误。这导致空白/白屏。

"mod_fcgid: stderr: PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 491520 bytes) in wp-content/plugins/w3-total-cache/lib/W3/ConfigKeys.php on line 1329"

“mod_fcgid:stderr:PHP 致命错误:第 1329 行的 wp-content/plugins/w3-total-cache/lib/W3/ConfigKeys.php 中允许的内存大小为 268435456 字节已耗尽(试图分配 491520 字节)”

"mod_fcgid: stderr: PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 491520 bytes) in wp-content/plugins/w3-total-cache/lib/W3/ConfigKeys.php on line 1329"

“mod_fcgid:stderr:PHP 致命错误:第 1329 行的 wp-content/plugins/w3-total-cache/lib/W3/ConfigKeys.php 中允许的内存大小为 268435456 字节已耗尽(试图分配 491520 字节)”

upping the memory_limit in the php.ini or .htaccess did not fix the issue for me. I had to go into the php settings for the domain and turn the safe mode option from "default" or "off" to "on" using Plesk.

提高 php.ini 或 .htaccess 中的 memory_limit 并没有解决我的问题。我必须进入域的 php 设置并使用 Plesk 将安全模式选项从“默认”或“关闭”转为“开启”。