PHP out of memory error even though memory_limit not reached

This isn't an answer to why your script is dying after a certain memory usage, but you can get around it by removing the memory limit entirely within the PHP script itself:

ini_set('memory_limit', '-1');

This is dangerous. If you have a runaway script, PHP will take memory until your server has none left to allocate and falls over. So you should only use this if you're sure the script itself isn't a problem, and only to test the output.

As to if PHP has some per-script limit on memory usage, no. I have personally run scripts with near 1GB memory usage.


I have finally found the answer. The clue came from pcguru's answer beginning 'Since the server has only 1 GB of RAM...'.

On a hunch I looked to see whether Apache had memory limits of its own as those were likely to affect PHP's ability to allocate memory. Right at the top of httpd.conf I found this statement: RLimitMEM 204535125

This is put there by whm/cpanel. According to the following webpage whm/cpanel incorrectly calculates its value on a virtual server... http://forums.jaguarpc.com/vps-dedicated/17341-apache-memory-limit-rlimitmem.html

The script that runs out of memory gets most of the way through, so I increased RLimitMEM to 268435456 (256 MB) and reran the script. It completed its array merge and produced the csv file for download.

ETA: After further reading about RLimitMEM and RLimitCPU I decided to remove them from httpd.conf. This allows ini_set('memory_limit','###M') to work, and I now give that particular script the extra memory it needs. I also doubled the RAM on that server.

Thank you to everyone for your help in detecting this rather thorny issue, and especially to pcguru who came up with the vital clue that got me to the solution.


This may not be the answer to your problem, but if you run PHP from the command line you can overrite the memory limit from php.ini.

php -d memory_limit=321M my_script.php

I'm not exactly sure what the default memory limit via cli is.

Also, you can run php --ini and check the results.


Since the server has only 1 GB of RAM I'm leaning towards the possibility that you have actually run out of system memory entirely.

See this thread. You get the same "PHP Fatal error: Out of memory" instead of the more common "Fatal error: Allowed memory size of ...". Your error indicates the system cannot allocate more memory at all, meaning even PHP:s internal functions cannot allocate more memory, let alone your own code.

How is PHP configured to run with Apache? As a module or as CGI? How many PHP processes can you have running at the same time? Do you have swap space available?

If you use PHP as a module in Apache, Apache has a nasty habit of keeping memory that the PHP process allocated. Guessing since it can't restart the PHP module in the worker, just restart the worker entirely. Each worker that has served PHP simply grows to the PHP memory limit over time as that worker serves a script that allocates a lot of RAM. So if you have many workers at the same time, each using 100 MB+, you will quickly run out of RAM. Try limiting the number of simultaneous workers in Apache.