Page 1 of 1

Fatal error: Allowed memory size of 262144 bytes

Posted: Thu Dec 02, 2010 9:49 pm
by beckpm
pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I'm getting a fatal memory error and have been unable to figure out what is causing it. To make it worse, the allowed memory size does not appear to be related in any way to the memory_limit value. Here's the information:

Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 128 bytes) in template.php on line 164
Excerpt from template.php (line 164 is where $max is assigned):
...

Code: Select all

else if(isDateType($type) || isTimestampType($type) || isDatetimeType($type))
{
    $min = htmlentities($countpost['constraints'][$tablename][$fieldname]['min'], ENT_QUOTES);
    $max = htmlentities($countpost['constraints'][$tablename][$fieldname]['max'], ENT_QUOTES);
    echo "\t\tFrom: <input name=\"constraints[$tablename][$fieldname][min]\""
        ." type=text value=\"$min\" onclickXSSCleaned=\"toggleCalendar(this)\" readonly>\n";
    echo "\t\tTo: <input name=\"constraints[$tablename][$fieldname][max]\""
        ." type=text value=\"$max\" onclickXSSCleaned=\"toggleCalendar(this)\" readonly>\n";
}
...

These two lines were included at the top of the script and printed "Base Memory Usage: 473172 Memory Limit: -1":
echo "Base Memory Usage: ". memory_get_usage() ."\n";
echo "Memory Limit: ". ini_get('memory_limit') ."\n";
I also get the same error in a slightly different location:
Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 1 bytes) in template.php on line 153
Excerpt from template.php (line 153 is the simple variable assignment of an empty string):
...

Code: Select all

if(!isset($countpost['constraints'][$tablename][$fieldname]['value']))
    $countpost['constraints'][$tablename][$fieldname]['value'] = "";
...

If I refresh the page repeatedly, the error will predictably occur at the same place. Once I change any code (even slightly), the location of the error will change to a new location, sometimes into an included file. The size of "tried to allocate X bytes" also changes. The only constant in this whole mess is the allowed memory size (which is ALWAYS 262144, no matter what the actual memory limit is).

I had previously set memory_limit to -1 in the php.ini file, which seems to have no effect on the error. I am not using a .htaccess file. Changing the memory_limit doesn't change the "allowed memory size" whether it's set to -1, 16M, or whatever.

This is using php 5.3.2, apache2 2.2.14, on Ubuntu 10.04. Any insights would be greatly appreciated.
Pat


pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Fatal error: Allowed memory size of 262144 bytes

Posted: Thu Dec 02, 2010 11:25 pm
by requinix
If you modify php.ini you have to restart Apache for it to take effect.

Most likely: something in the script, before that point, sucked up a lot of memory. There's also a chance of a memory leak but that's unlikely.
Use memory_get_usage in a number of places to see what happens with memory. Look for a big jump.

Re: Fatal error: Allowed memory size of 262144 bytes

Posted: Thu Dec 02, 2010 11:43 pm
by beckpm
I've restarted apache after each change in php.ini.

I have memory_get_usage in the code several times to watch the memory. There are small changes, and a change of about 300k when I first load another file. The max gets up to 1048576 bytes (memory_get_peak_usage). 1 Mb does not seem very large to me and is much smaller than the memory_limit.

Thanks for the feedback. I'll give any more details needed to solve this.

Re: Fatal error: Allowed memory size of 262144 bytes

Posted: Fri Dec 10, 2010 12:45 am
by beckpm
Today I wrote a script in php to just use up memory as a test of my php and apache installations. It got up to 128M without any problem. Obviously, it's a problem in my code.