1

I have set my PHP MAMP server to a maximum of "256M" in the php.ini but am suddenly getting a memory error. There are two things that are strange about this:

  1. This is a shockingly high amount of memory for a script to use. Maybe I'm too easily shocked but should scripts be running over 256M? I wouldn't have thought so.
  2. Even though I've now changed my setting to "512M" I still get the memory error and it still reports running out in/around the 256M range (as if my setting is being ignored). I did recycle the web server which I figured would be enough to get the new settings into play.

The exact message I'm getting is this:

[25-Oct-2012 14:27:53] PHP Fatal error:  Allowed memory size of 268435456 bytes exhausted (tried to allocate 222300161 bytes) in [FILE_PATH]/wp-content/themes/lifegadget-pagelines/sections/lg_content/section.php on line 42
[25-Oct-2012 14:27:53] PHP Fatal error:  Call to a member function get() on a non-object in [FILE_PATH]/wp-content/object-cache.php on line 52

So my questions are as follows:

  1. Troubleshooting. Is there any way to troubleshoot memory usage?
  2. Scale. Is exceeding a 256M limit a clear problem or is this justified in some cases?
  3. New Ceiling. Why is my new ceiling of 512M being ignored? Not that I'm happy to have it stay at 512M but I would have thought it would at least get me back up and running (I have 16GB on the machine so there's plenty of physical memory).
ken
  • 7,647
  • 9
  • 56
  • 117
  • The short answer to your first question is no :( http://stackoverflow.com/questions/849549/finding-cause-of-memory-leaks-in-large-php-stacks. It's hard to answer your second question without context but there's always an appropriate situation where more memory is an acceptable solution (the reverse is also true). – Mike B Oct 25 '12 at 14:58
  • 1
    Depends entirely on what your script is trying to do. a simple `echo 1+1` script would be shocking if it used up 256meg. A script which loads a gigapixel image into GD for processing would be shocking if it DIDN'T use multi-megabytes of memory. – Marc B Oct 25 '12 at 14:59
  • 1
    Use [memory_get_usage](http://php.net/memory_get_usage) at various places (where you believe the script might use more memory) in your script and either directly `echo` the values on your webpage or log them into a file. What is your script doing? Is it possible for you to post the source code? Or maybe a relevant part of it? – Jocelyn Oct 25 '12 at 17:07
  • 1
    I'd recommend taking a look at Xdebug. I can almost promise you that you'll find the leak. – Karl Laurentius Roos Oct 25 '12 at 20:50
  • It appears to be happening when I'm building up an associative array of values that came from the database. Using the memory_get_usage/memory_get_peak_usage functions I can see that adding an additional table of results to my query causes the fatal error but in this failure condition I still haven't come close to the supposed memory ceiling. For example right before failing i get a current memory of 33,045,056 bytes, peak of 36,274,496 bytes and yet the Fatal errors says "Allowed memory size of 268,435,456 bytes exhausted". – ken Oct 26 '12 at 12:10
  • Also strangely I'm noticing that running exactly the same script, with exactly the same result set coming back from the database has a very different memory usage from transaction to transaction. The difference shows up almost immediately in the script execution where in some cases it is 22mb and others only 4.5mb. Does this make any sense? The variation in memory usage after this starting footprint is also effected by the starting point. If it started at 22mb it grows to around 42mb, if starting at 4.5mb then it grows to about 12mb. – ken Oct 26 '12 at 14:32

2 Answers2

0

Infinite loop can cause this or if you have circular references with pre 5.3 PHP version. See: Garbage collection

Peter Kiss
  • 9,153
  • 2
  • 21
  • 38
0

Inject this function ini_set('memory_limit', -1); at the very beginning of your suspected memory leaking class/page, this is a temporary solution to get things working but it won't help you know where exactly your script is leaking memory.

HaniGamal
  • 533
  • 4
  • 7