Php – Apache httpd processes and PHP out of memory

apache-2.2httpdmemoryPHPserver-crashes

I have a VPS running apache-php-mysql on centos and a single drupal website installed.
The VPS has 256MB of RAM (could be the root cause of all my problems… maybe I just need more).
Whenever I try to open my website from multiple browser tabs (about 8… not 800) all at once, apache crashes!
I have this on the log:

[Wed Oct 24 11:26:31 2012] [error] [client xxx] PHP Fatal error: Out of memory (allocated 28049408) (tried to allocate 201335 bytes) in xxx on line 2139, referer: xxx

I have read many many posts here, but I think there is something fundamental that I'm missing –
If I understand correctly some php script tried to allocate 200K after allocating 28MB, and fails to do so.

First question is: should this cause the apache to crash???

Next, I tried to look at 'top' command while I do my little test.
Indeed I see 7 httpd processes, each reserving about 30MB – which explains why my RAM runs out.

How do I prevent apache from creating new processes until it's out of memory?

I tried configuring /etc/httpd/conf/httpd.conf like this:

<IfModule prefork.c>
StartServers       1
MinSpareServers    1
MaxSpareServers    1
ServerLimit        1
MaxClients         1
MaxRequestsPerChild  100
</IfModule>

But got the same exact result!

What am I missing?

Thanks a lot!

Update:

My PHP memory_limit is 128M (confirmed by the drupal admin pages…)
The output of free -m:

             total       used       free     shared    buffers     cached
Mem:           256        226         29          0          0          0
-/+ buffers/cache:        226         29
Swap:            0          0          0

What can I do to make apache consume less memory?
Can't I save memory on the expense of slower responses?

Best Answer

Depending on your version of Drupal: Drupal 6 core requires PHP's memory_limit to be at least 16MB. Drupal 7 core requires 32MB.

Based off of the error and your post you've allocated 28MB, please confirm in the php.ini file that memory_limit is set correctly. Make sure to restart Apache to apply any changes to php.ini.

Another aspect to consider is the other services that are running on your server, Apache, MySQL, etc. These also take from the total 256MB.

To get an idea on the current memory usage you can type:

free -m

The -m flag outputs the data in MB. The -b switch displays the amount of memory in bytes; the -k switch (set by default) displays it in kilobytes.

You'll get an output like:

total       used       free     shared    buffers     cached
Mem:         24031      22512       1518        0       1614       8082
-/+ buffers/cache:      12815      11215
Swap:        31999        485      31514

Depending on the available memory you can begin to narrow down the issue to either PHP or if you've outgrown/maxed out the VPS.

EDIT

@Ofri,

There's no exact science to the Apache config.. It boils down to knowing the specs of the machine and mainly trial & error. Monitoring performance of the server during the on-going trial and error periods is important so you can see the postive/negative impact.

I would stop Apache, edit the httpd.conf according with the values below and see how the server behaves:

  • MaxKeepAliveRequests 100
  • KeepAliveTimeout 15
  • MinSpareServers 5
  • MaxSpareServers 10
  • StartServers 5
  • MaxClients 150
  • MaxRequestsPerChild 300

Keep in mind Drupal is a system hog and you may ultimately end up upgrading the VPS to 512MB.

-Brendan