Yet another Apache2 memory thread

Noob here,

I ordered my Linode a little less than a week ago, and so far mail, connections, etc are running excellent(ly?).

And then there's Apache.

Tasks: 146 total,   2 running, 144 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.1%sy,  0.0%ni, 99.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.1%st
Mem:    553168k total,   397456k used,   155712k free,    43276k buffers
Swap:   262136k total,    49804k used,   212332k free,   179100k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
 2292 mysql     18   0  130m  11m 2896 S    0  2.0   2:03.66 mysqld             
15731 oratos    17   0 27536  12m 3952 S    0  2.4   0:29.73 php5-cgi           
16073 oratos    16   0 26524  11m 3704 S    0  2.2   0:30.48 php5-cgi           
16072 oratos    16   0 26512  11m 3940 S    0  2.2   0:30.93 php5-cgi           
16075 oratos    16   0 26512  11m 3708 S    0  2.2   0:30.24 php5-cgi           
15730 oratos    16   0 26508  11m 3996 S    0  2.2   0:28.24 php5-cgi           
16074 oratos    16   0 26508  11m 3728 S    0  2.2   0:30.99 php5-cgi           
15732 oratos    17   0 26468  11m 3740 S    0  2.2   0:28.53 php5-cgi           
15729 oratos    16   0 26268  11m 3676 S    0  2.1   0:29.42 php5-cgi           
15840 jacob     16   0 24892  11m 5332 S    0  2.2   0:03.87 php5-cgi           
16071 oratos    17   0 20492 5764 3900 S    0  1.0   0:00.01 php5-cgi           
15728 oratos    17   0 20488 5760 3900 S    0  1.0   0:00.01 php5-cgi           
23988 www-data  18   0 12128 3552 1440 S    0  0.6   0:00.03 apache2            
24034 www-data  15   0 12128 3508 1392 S    0  0.6   0:00.01 apache2            
24038 www-data  15   0 12128 3516 1392 S    0  0.6   0:00.00 apache2            
24077 www-data  15   0 12128 3548 1440 S    0  0.6   0:00.01 apache2            
24080 www-data  15   0 12128 3508 1392 S    0  0.6   0:00.00 apache2            
24084 www-data  15   0 12128 3548 1424 S    0  0.6   0:00.00 apache2            
24090 www-data  15   0 12128 3508 1392 S    0  0.6   0:00.00 apache2            
24094 www-data  15   0 12128 3508 1392 S    0  0.6   0:00.00 apache2            
24101 www-data  15   0 12128 3504 1392 S    0  0.6   0:00.00 apache2            
24102 www-data  15   0 12128 3508 1392 S    0  0.6   0:00.00 apache2            
24112 www-data  15   0 12128 3504 1392 S    0  0.6   0:00.00 apache2            
24137 www-data  15   0 12128 3504 1392 S    0  0.6   0:00.00 apache2            
24136 www-data  15   0 12120 3508 1392 S    0  0.6   0:00.00 apache2            
24081 www-data  15   0 12068 3500 1424 S    0  0.6   0:00.00 apache2            
24058 www-data  15   0 12040 3440 1424 S    0  0.6   0:00.00 apache2            
24089 www-data  15   0 11992 3304 1392 S    0  0.6   0:00.00 apache2            
24108 www-data  15   0 11992 3304 1392 S    0  0.6   0:00.00 apache2            
24129 www-data  15   0 11992 3304 1392 S    0  0.6   0:00.00 apache2            
24133 www-data  15   0 11992 3300 1392 S    0  0.6   0:00.00 apache2            
24142 www-data  15   0 11992 3292 1388 S    0  0.6   0:00.00 apache2            
24149 www-data  15   0 11992 3288 1384 S    0  0.6   0:00.00 apache2            
24152 www-data  15   0 11992 3288 1384 S    0  0.6   0:00.00 apache2            
24156 www-data  15   0 11992 3304 1392 S    0  0.6   0:00.00 apache2            
24157 www-data  15   0 11992 3288 1384 S    0  0.6   0:00.00 apache2            
24158 www-data  15   0 11992 3292 1384 S    0  0.6   0:00.00 apache2            
24159 www-data  15   0 11992 3300 1392 S    0  0.6   0:00.00 apache2            
24160 www-data  15   0 11992 3300 1392 S    0  0.6   0:00.00 apache2            
24144 www-data  15   0 11856 3064 1280 S    0  0.6   0:00.00 apache2            
...
... 

Granted, this was during a digging. I don't care about the php5-cgi (suexec'd) processes; they can do what they want. But Apache is using over 40 processes to get the job done. The sites run fast, but I'd like to get this memory usage down.

Currently, I'm using apache2-mpm-prefork. Last night, when the server was idle, I tried out mpm-worker, due to the loud cries of "low memory usage!" "fast servers!" "threads!". I did like the fact that only 4 apache2 processes were running. I didn't like that two of them were using 230MB or RAM… That's a bit much, especially for being idle.

Right now hosting fosswire.com on mpm-prefork during peak traffic uses less RAM than mpm-worker, but even then I have a ton of apache2 processes.

I tried Lighttpd and love it, but I'm not one to be converting the plethora of .htaccess configs over. Or for trying to get suexec to work on it. :shock:

Is Apache made to eat up all available memory to reserve it for web use, or is there something wrong here? I've been reading the forums here and I see some people have it running at <64MB. What am I missing?

Jacob

3 Replies

Some thoughts:

1. You don't seem to be swapping much, and there is plenty of free+buffer, so the apache processes aren't actually doing much harm, unless you've got something else you want to use the memory for.

2. For both the pre-fork and thread modules, a lot of that memory is most likely shared (more, I think, than what shows up under SHR). Note also that the resident value is much less than the virtual.

3. On a RAM limited system, you can actually get more work done with fewer processes, because you avoid swapping. Besides lowering the process/thread limit, you also need to reduce the keep-alive (like to 5 or 10 seconds, max), so you don't have processes holding open connections that will never be used. You need to look at the number of actual active apache2 processes/threads, which one of the built-in statistics pages will tell you (I don't recall the name off-hand).

@SteveG:

2. For both the pre-fork and thread modules, a lot of that memory is most likely shared (more, I think, than what shows up under SHR). Note also that the resident value is much less than the virtual.
Ah, I never really noticed the difference with resident and virtual. I'll keep that in mind.

> 3. On a RAM limited system, you can actually get more work done with fewer processes, because you avoid swapping. Besides lowering the process/thread limit, you also need to reduce the keep-alive (like to 5 or 10 seconds, max), so you don't have processes holding open connections that will never be used. You need to look at the number of actual active apache2 processes/threads, which one of the built-in statistics pages will tell you (I don't recall the name off-hand). I couldn't find the specific command to look it up, but I installed htop for a tree view of things to get a better layout. Tuned some thread settings and it seems to be using a lot less right now. Setting KeepAliveTimeout 5 also made quite an impact.

For the most part, this is a little better. It seems a little high still, but then again I have yet to check my modules. Thanks for the tips, they definitely helped!

The status page I was thinking of is supported by modstatus: http://httpd.apache.org/docs/2.2/mod/modstatus.html. It's alot more informative than just htop, because it shows what the workers are actually doing, if anything.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct