Thursday, October 28, 2010

Joomla PHP Fatal error: Out of memory

Hi Friends,

Few days back i was playing with an internal setup of WAMP Server having Apache 2.2.11, PHP 5.3.0 & MySQL 5.1.36 deployed with Joomla 1.5.21 CMS.

The first & foremost problem i faced on daily basis was a Apache Crash due to PHP Fatal error: Out of Memory!

Googling around pointed me to increase the memory allocated to PHP in php.ini by changing the parameter
; Maximum amount of memory a script may consume (128MB)
memory_limit 128M
By default, PHP 5.3.0 has 128MB memory allocated. The error logs though pointed to a out of memory crash, it actually didnt have a memory availability issue.

I changed this to 256MB, 512MB & even 1024MB but all the time it crashed :-(
The official parameter description suggested that, if you set this up to -1, there wont be an upper limit capped for PHP.

My next setup test was to change it to -1
; Maximum amount of memory a script may consume (128MB)

memory_limit -1

Well, i wasnt surprised as it still crashed!!
After much digging, i felt that Apache itself wasnt able to handle its memory well on Windows. Many articles i came across also suggested the same.

So in the quest of stablizing Apache on Windows, i started investigating this issue purely from Apache point of view.
An quick overview of mpm_winnt_module parameters on suggested that i needed to have a couple of must needed configurations :-)
  1. ThreadLimit which has a default value of 2500 if not specified, was brought down to 1000
  2. ThreadsPerChild which has a default value of 64 needs to be increased as i had atleast a 500  concurrent user base, so this was changed to 500. Remember, ThreadLimit & ThreadsPerChild go in tandem. You cannot lower ThreadLimit value when compared to ThreadsPerChild.
  3. MaxRequestsPerChild which has a default value of 0, need not be changed as in Windows, we have one parent process managing the other child process which caters to multiple threads. No separate child process forking as possible on Linux/Unix deployments.
  4. KeepAlive should be turned ON. It is very necessary to avoid unnecessary new connections from the same client within a span of 1 minute. The timeout should be turned on with atleast 1 minute.
  5. KeepAliveTimeout which has a default value of 15secs needs to be changed to 60secs i.e. 1minute
  6. MaxMemFree is a directive available in Apache 2.2.x which allows us to specify upto how much free memory will be held by the Apache process without calling the free() function. When not specified this is considered as unlimited, which is generally not good for Apache deployed on Windows Platform as memory leaks are evident. I changed this parameter to 100 in order to force Apache release free memory frequently & avoid leaks. Remember this settings has direct impact to your webserver performance, but if you are seeing too many frequent crashes, this will definitely stabilize the system.
Thats it!
Putting above settings in place along with php memory limit to -1, i was successful in stabilizing the PHP Fatal error + Apache crashes.

Please note that extensive testing was done to arrive at this settings, hence be sure of your implementation requirements & sizing before adopting ;-)

Apache, WAMP, PHP, MySQL, mpm_winnt_module, Joomla, performance tuning, etc.

No comments: