Apache is using too much of my server memory causing it to crash. I have 4GB of RAM in the server.
I'm trying to fine tune Apache settings in order to improve it's performance but I'm quite new at this.
I was trying to follow this article's advice but I'm not sure how to calculate things and it seems I'm making it worse.
My top reads like:
11697 apache 15 0 322m 37m 4048 S 0.0 0.9 0:00.52 httpd
13602 apache 15 0 323m 37m 3944 S 0.0 0.9 0:00.50 httpd
11786 apache 15 0 322m 36m 4052 S 0.0 0.9 0:00.50 httpd
12525 apache 15 0 322m 36m 4040 S 0.0 0.9 0:00.63 httpd
11806 apache 15 0 322m 36m 3952 S 0.0 0.9 0:00.42 httpd
11731 apache 15 0 322m 36m 4036 S 0.0 0.9 0:00.46 httpd
11717 apache 16 0 322m 36m 3956 S 0.0 0.9 0:00.54 httpd
11659 apache 15 0 322m 36m 3980 S 0.0 0.9 0:00.49 httpd
So, it would be
MaxClients = 3000/ (322-37) = 10
Is that right? Also, what should be the values for the other parameters such as MinSpareServers, MaxSpareServers,MaxRequestsPerChild, StartServers, MinSpareThreads, MaxSpareThreads, ThreadsPerChild, MaxRequestsPerChild ?
Would someone please help me?
Update
I've tried what you guys have suggested. It works, but just for a while. After some time after the server is started memory usage keeps increasing and never goes down.
I mean, after I start the server, let's assume there'are 500 users online. Server will consume X RAM. 2 hours after that, with the same 500 users online, server will be consuming 10X RAM.
Is there a way to avoid this or I'll have to keep watching the server and restarting it from time to time?
Best Answer
The main parameter for tweaking Apache's memory usage will be
MaxClients
. Too low a value and you will run out of available slots to serve client requests. Too many and you will use up all your RAM and begin to use swap space which will kill performance (it may appear to be a server crash).One way of tuning
MaxClients
is to observe the system's memory usage and tweak the setting up/down as needed. If the server begins to swap edit it down. If the server has free memory put it up.You can also estimate the maximum value by looking at Apache's memory usage. Start
top
and pressM
to sort processes by memory. You should see something like:Subtract the RES and SHR columns to get the approximate memory usage per Apache instance. In this case it is around 16MB. If I have 4GB of RAM and wish 3GB of it to be used for Apache my MaxClients setting will be around:
So, in this case, I might start with a value of 150-200 but I would watch the memory usage and if it ever began to get close to using swap I would decrease MaxClients 10-20%. Also note that the value of 3GB is just a random example. On servers only running Apache I might be able to use almost all of the 4GB. In other cases I might only want 1 or 2GB for Apache save the remaining for other applications, the system or the cache.
Edit: Answering Additional Questions
There are generally no magic values of MaxClients or the other Apache configuration parameters that will make your server suddenly twice as fast. Some servers will appear to run just fine whether MaxClients is 10 or 1000. There are two main cases where the MaxClients setting is "bad":
While I don't know the details of your server, application, or traffic I can suggest the following configuration values as a starting point. Try them, monitor the server's load and usage, and change settings as needed.