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

29k views Asked by At

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.

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 ?

3

There are 3 answers

5
zeantsoi On BEST ANSWER

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

1
Eric On

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 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"

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.

1
Kzqai On

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:

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:

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.