Apache using all 16 GB Memory, how to limit its processes and memory usage?

情到浓时终转凉″ 提交于 2021-02-07 04:32:25

问题


We are on 16GB AWS instance and I am finding it to be really slow. When I ran

ps -aux | grep apache

I can see about 60+ apache processes.

When I ran

watch -n 1 "echo -n 'Apache Processes: ' && ps -C apache2 --no-headers | wc -l && free -m"

It is showing almost all memory being used by apache.

When I ran

curl -L https://raw.githubusercontent.com/richardforth/apache2buddy/master/apache2buddy.pl | perl

to see how to optimize Apache, it suggested me to increase number of MaxRequestWorkers so I made it 550. I also changed MaxConnectionsPerChild from 0 (unlimited) to 10000.

Here is my /etc/apache2/mods-enabled/mpm_prefork.conf file

<IfModule mpm_prefork_module>
        StartServers              5
        MinSpareServers           5
        MaxSpareServers          10
        MaxRequestWorkers        550
        MaxConnectionsPerChild   10000
</IfModule>

Can you tell me how can we optimize apache memory usage so it don't bring down the whole site down ?


回答1:


I had a similar problem with an instance in EC2 and here's what I did and would suggest:

  1. If you are using prefork, make sure that the module is loaded by typing these two commands apache2 -l and sudo apache2 -M If you can see the prefork module loaded in the results of either of these two commands then up to the next step. Otherwise, make sure to load it first or else you would be changing the configurations for nothing.

  2. Run this command to find the average memory each apache2 process is using ps aux | grep 'apache2' | awk '{print $6/1024;}' | awk '{avg += ($1 - avg) / NR;} END {print avg " MB";}' Call that value x

  3. Restart your apache server by using sudo service apache2 restart and take a note of how much free memory you have. What I did was subtract an extra 200MB-500MB cushion from that free memory to be used later. Call that value y

  4. Divide the value of free memory y over the amount of memory used per process x and that would be the value of MaxRequestWorkers = y/x

  5. As for the value of MaxConnectionsPerChild then you can tweak it till you get the right configuration. It you make it too big, then the process will keep using more and more memory before being killed. If you make it too small, then the processes will die too quickly and that will present an overhead on your system. I usually keep it somewhere between 4000 and 10000.

  6. Some of these steps have been taken from the accepted answer in the following link: StackExchange: httpd memory usage where one solution also suggested disabling some of the modules if you don't need them.

I would suggest you do steps 1-5 first and see if that solves your problem!

Good luck!



来源:https://stackoverflow.com/questions/41015166/apache-using-all-16-gb-memory-how-to-limit-its-processes-and-memory-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!