Running out of memory, but allowed is so much higher - php

Getting the following error, which I understand and have seen before. But one thing seems weird, it is trying to allocate an amount which is lower the the allowed memory size. Doesn't this seem weird?
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 7680 bytes) in XXX on line 93
Could this be some kind of misconfiguration with memory management, I am using a cloud server.

The number "tried to allocate 7680 bytes" does not mean the total amount of memory the script allocates, but the last portion on which it exceeded the limit.
So let's say your script already allocated 33550000 b, tries to allocate another 7680 b. The total exceeds the memory limit and that's why the error appears.

Maybe the memory limit is set smaller than what you're thinking it is set to?
You can use this to display the current limit
echo ini_get('memory_limit');
or increase it inline with
ini_set('memory_limit', '64M');
Of course you can see the entire php enviornment with
echo phpinfo();

Related

session_start() - cause E_ERROR - Allowed memory size of 134217728 bytes exhausted

In one of application that I maintenance (PHP based) I got the E_ERROR which is connected to the memory - "Allowed memory size of 134217728 bytes exhausted...".
I am familiar with temporary solutions like setting up higher amount of memory for the script etc. but it is just temporary solution and here I need to solve it from the roots.
I have checked amount of memory which is used after each part of code in my script by simply writing it to the log file like:
addMsg('1 - Memory Usage: ' . (memory_get_usage()/1048576) . ' MB \n');
By this method I have found out that only one simple line of code increase drastically memory usage and it is: session_start() function, which then causes this error.
Interesting thing is that this error does not occur every time scripts loads. It occurs from time to time (based on high traffic on that app, it occurs every few minutes). Also, I have found out about this error from New Relic which I use for monitoring and from logs. Although, I have never experienced myself.
Does anybody have any theoretical idea how this simple function can cause such memory usage that it cause E_ERROR?
Also, all of my errors look something like this:
Allowed memory size of 134217728 bytes exhausted (tried to allocate 10556354 bytes) in Unknown on line 0
Allowed memory size of 134217728 bytes exhausted (tried to allocate 10399243 bytes) in Unknown on line 0
Allowed memory size of 134217728 bytes exhausted (tried to allocate 8112988 bytes) in Unknown on line 0
From my point of view, it seems that script wanted to allocate less space than it was allowed and even that, error occurred. Am I right or something else is going on?

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

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 ?
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.
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.
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.

Allowed memory size exhausted error when reading file

Help me, please.
When I'm trying to read a file using
$tmp = file('file.log');
I get an error
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 90 bytes) in script.php on line 37
php memory limit is 128M, size of file.log is only 48M.
It seems that something have consumed some amount of memory before. You allocate 128M for all operations, data and so on in your script. And memory has finished in this place only because your script exceeded it trying to sum, for example,
87M(uses by something earlier)+48M(your file).

Fatal error on memory size (over 64mb?)

I'm getting this error:
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 3456 bytes) in /home/gigiphot/public_html/Includes/thumbnail.inc.php on line 158
and this PHP memory limit is set to 64mb .. do I increase it? Not sure of next step...
You can set memory_limit in your php.ini file, see here: http://php.net/manual/en/ini.core.php
However, you may want to investigate if you really need such a heavy memory consumption... if this service will be used by multiple users concurrently, you may have problems... An unintended high memory consumption is often a symptom of something going wrong in your script.
Try adding ini_set('memory_limit', '128M'); to the top of the file.
You can set it to -1 to ignore the limit, but it's not generally recommended.

PHP using too much memory

I'm getting the following error when trying to run a php script I wrote:
Fatal error: Allowed memory size of
33554432 bytes exhausted (tried to
allocate 56320 bytes) in
/home/evergrf2/public_html/ianburris/p/maptile/mapfetcher.php
on line 43
What confuses me is that it says the allowed memory size is 33554432 bytes and that when the script tried to allocate 56320 bytes of space the allowed memory was exhausted. How is this possible when 56320 is less than 33554432? Maybe I'm misinterpreting what this is saying...
It says that trying to allocate additional 56320 bytes caused memory exhaustion (so it already had at least 33498112 bytes allocated).
allocation of 56320 pushed you above the limit. Increase your limit in php.ini if needed.
to be more clear dont read it as alocating 56320 is more than allowed 33554432. Instead read it as, while allocating 56320, we surpassed the limit of 33554432.
Modified: dont increase without properly debuging and making sure there are no memory leaks.
33554432 bytes is 32MB, which is not huge.
You can increase PHP's memory limit (in php.ini, look for a line that reads 'memory_limit = 32M' and modify it appropriately). I generally use 128M for development and heavy number-crunching.
The other solution is to profile and rewrite your code to use less memory.
I would also profile the script with the help of Xdebug,
to help find possible memory leaks.

Categories