Solve the problem that Apache occupies a large amount of memory for a long time, Apache memory optimization method

Q: Why does one Apache.exe in the process take up hundreds of megabytes of memory and does not decrease after the server runs continuously for several days or after the access peak?
Answer: Open apache2\conf\httpd.conf with Notepad,
I installed kloxo on centos5, and found httpd.conf in /etc/httpd/conf/ directory for a long time.
Find MaxRequestsPerChild and change MaxRequestsPerChild 0 to MaxRequestsPerChild 50.
 Several important parameters that affect apache performance (set in conf/httpd.conf)
KeepAlive Whether to allow continuous connections
MaxKeepAliveRequests The maximum number of continuous connections allowed
KeepAliveTimeout Continuous connections cut off after no requests for how many seconds
StartServers How many server processes are started at initial startup
MinSpareServers The minimum number of
idle server processes MaxSpareServers The maximum number of idle server processes
MaxClients The number of requests processed at the same time (the most important parameter, less than ServerLimit)
MaxRequestsPerChild The maximum number of requests processed by each child process
The relationship between them:
After the prefork control process initially establishes "StartServers" subprocesses, in order to meet the needs of the MinSpareServers settings, create a process, wait for one second, continue to create two, wait another second, continue to create four... This increases exponentially The number of processes created, up to a maximum of 32 per second, until the value set by MinSpareServers is met. This mode eliminates the need to generate a new process when a request arrives, thereby reducing system overhead and increasing performance. MaxSpareServers sets the maximum number of idle processes. If the number of idle processes is greater than this value, Apache will automatically kill some redundant processes. This value should not be set too large, but if the value is set smaller than MinSpareServers, Apache will automatically adjust it to MinSpareServers+1. If the site load is heavy, consider increasing MinSpareServers and MaxSpareServers at the same time. MaxRequestsPerChild sets the number of requests that each child process can handle. Each child process will be automatically destroyed after processing "MaxRequestsPerChild" requests. 0 means infinite, i.e. the child process is never destroyed. Although the default setting of 0 allows each child process to handle more requests, if it is set to a non-zero value, it also has two important benefits: 1. It prevents accidental memory leaks. 2. When the server load drops, the number of child processes will be automatically reduced.

  ServerLimit 2000
  StartServers 10
  MinSpareServers 10
  MaxSpareServers 15
  MaxClients 1000
  MaxRequestsPerChild 2048

Commands used during debugging:
# ps -ef|grep http|wc -l //View the total number of requests
# cat /proc/loadavg //Check the average load (loadavg), loadavg is higher than 1, indicating that the task queue is waiting, and the CPU is too busy. If it exceeds 2 or more, the performance will obviously be reduced.
# netstat -ant | grep :80 | wc -l //Check the number of TCP connections
# top //Check the system operation
============== ===================================================== ====
apache's memory usage The
apache process "increases" when it uses memory. That is to say, until the process dies, the amount of memory used is always growing and not decreasing. In this case, the amount of memory used by the apache process depends on the maximum amount of memory used by your application.
keepalive parameter
KeepAliveTimeout This parameter determines how long an http process can wait before doing nothing? Imagine, if keepalive is set to on, and keepalivetimeout is set to a relatively large number, the memory occupied by apache will grow rapidly. This is because an apache process completes a task (and reaches a certain memory footprint, think about the "progressive" mode), and does not exit immediately, but waits for a keepalivetimeout time. Assuming that the user's link requests continue to come, the accumulated useless apache processes will be quite a lot, and these processes will not be killed until timeout.
However, keepalive is indeed very effective for the transmission of static files, such as image files. Therefore, keepalive should be set to on, (off) but keepalvietimeout should be set to a smaller value, such as 5s 15
MaxRequestsPerChild
这个参数是说,apache进程在处理了多少个请求之后,必须退出,重新开始,以免在处理中的内存问题。
对于php脚本来说,把这个参数设置的小一些是有好处的,可以避免程序使用的内存持续增长对apache带来的压力:让这个参数定期释放内存,因为php是在脚本执行完毕后,自动释放只用的资源(内存)的。
比如设置为50?如果太小的话,重新产生一个apache进程也是要消耗资源的,这是一个平衡问题

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326919429&siteId=291194637