Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP out of memory error even though memory_limit not reached

Tags:

I have just inherited a site with a PHP script that is consistently running out of memory at 117 MB. This happens even when I increase PHP's memory_limit variable to 312 MB, which I'm doing via php.ini.

This is now solved thanks to a great clue from pcguru. See my answer below that begins: I have finally found the answer

ini_get('memory_limit') returns the value set in php.ini, so I'm certain Apache has restarted after changing the value. I'm using memory_get_usage(true) to return the memory consumed by the script at various points along the way. And it's consistently failing when it gets to 117 MB.

Is there some internal PHP limit I'm unaware of that has it never allocate more than 117MB to an individual script?

The server has 1GB of RAM and is running CentOS. I have root shell access. PHP is version 5.3.18. MySQL is version 5.1.66-cll.

This script is behind a username/password and I can't provide public access to it.

Edited to Add:

1) Thanks all for your help to date. You'll find more info in my replies to specific user comments under various answers below.

2) Suhosin is definitely not installed. I've checked in multiple places including running a script and check for constants and running php -v

3) The apache log has no record of the specific error message I'm getting. Logging is switched on in php.ini. I piped through grep to search the entire log.

4) Is it possible the wrong error is being reported in this case?

like image 756
user8109 Avatar asked Dec 19 '12 15:12

user8109


People also ask

How do I fix out of memory error in PHP?

Create a phpinfo. php file under root directory and check for current memory limits. By default memory limit is 8M, but in this case you have to increase the memory limits to 12M, 16M, 24M and so on with this line. This will increase your memory limit and solve this error.

What is Memory_limit in PHP INI?

The PHP memory_limit is the maximum amount of server memory that each PHP script is allowed to consume. Per the PHP documentation: “This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts from eating up all available memory on a server.”

What does error code out of memory mean?

What does error code out of memory mean? This error implies that the resources or memory available in the Edge browser are insufficient to obtain and open the requested web page. In other words, we can say you are out of memory, or there is a Microsoft Edge memory leak.


2 Answers

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.

like image 82
user8109 Avatar answered Sep 19 '22 04:09

user8109


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.

like image 39
oldwizard Avatar answered Sep 22 '22 04:09

oldwizard