Limit Apache 2 Memory Usage
I am running a hobby webserver off of an ancient Blue & White G3/300 running Debian PPC Squeeze 2.6.30. The performance is okay for a while after a restart, but it eventually gets more and more bogged down. Right now it's at 76 days uptime, and the main culprit seems to be the memory usage of 10+ apache2 processes.
I think I need to lower the values for StartServers
, MinSpareServers
, and/or MaxSpareServers
, but I'm not sure which one to adjust, and there are three sections for each depending on which mpm module is in use.
How do I tell which of the following sections I need to change, and what are some reasonable values given that the box has 448 MB physical memory (weird upgrade history of one each 64, 128, and 256 sticks)?
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_event_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
There aren't any other instances of StartServers in my apache2.conf, but none of those mpm modules appear in mods-available
or mods-enabled
. Ideas?
Thanks!
Solution 1:
400MB isn't much for a web server these days -- you may want to consider replacing the box :)
That said, if your memory usage is ballooning you probably have a memory leak somewhere -- for a quick test look at the size of the httpd processes now, then stop/start Apache & see if they're a lot smaller. If they are watch them for a few days and see if they grow.
If it is a memory leak the real solution for that is to find and fix the memory leak, but since that's usually a pain in the ass you can also adjust MaxRequestsPerChild
to something other than 0 (unlimited). This will kill off the Apache workers when they've serviced a fixed number of requests (forcing them to give up their leaked memory in the process.
Start with larger values (in the thousands or so) and work your way down into the hundreds. If you get below 100 requests per child your memory leak is big enough to warrant actually fixing it as the performance hit from constantly killing off and re-spawning apache workers will be significant.
Re: which mpm to adjust, the answer is almost certainly prefork
.
You can run httpd -V
and look for the Server MPM:
line which will tell you for sure.
Solution 2:
To nail down the memory use of the apache process you will want to adjust the MaxClients.
The common rule of thumb is: (Max desired Memory) / (Memory Usage of 1 Apache Process) = # Max Clients
In the end you also need to adjust your to be MaxSpareServers <= Max Clients.
The memory usage of one process is strongly depended on the modules you have loaded (php and so on) so you may want to set the Max Clients a little lower.