Nginx + php-fmp memory is not released

Source: Internet
Author: User
Nginx + php-fmp memory is not released because the server only has MB of memory, and a concurrent php application is running on the server. after running for a period of time, it is found that the memory continues to grow, one part is the growth of PageCache, which can be released through echo3 & amp; gt;/proc/sys/v nginx + php-fmp memory

Because the server only has MB of memory, a php application running a concurrency is not very large. after running for a period of time, it is found that the memory continues to grow, some of which are the growth of PageCache, this can be effectively released using the echo 3>/proc/sys/vm/drop_caches command. the memory of another application increases and cannot be released,Through top naming (input the name and press the m key to sort by the memory size occupied by the process), we found that most of the memory is occupied by php-fpm. I checked it online, generally, php-fpm recycles the memory after processing each php request, but does not release it to the operating system, resulting in a large amount of memory occupied by php-fpm.The solution is to adjust the pm in the php-fpm.conf. max_requests value, pm. the role of max_requests is to restart the thread after sending the number of requests so that it can release the occupied memory. the default value is 0, that is, never restart. In my application, I set this value to 250. after observing for a while, I found that there are still several php-fpm instances that occupy about 2.8% of the memory, and the effect is not satisfactory, the possible reason is that the request volume is large, or the threshold cannot be reached. when we set it to 10, we find that php-fpm will be continuously restarted. I personally think this configuration is not very good, allSet this value to 250, and use a script to kill php-fpm.In this way, the memory occupied by the instance is released. In addition, if the system load is huge, restarting the instance will cause the cpu usage to soar. The script is as follows:
#! /Bin/bash
Log =/home/www/scripts/php-fpm.log
Echo 'date "+ % F % H: % M: % S" '> $ log
For PID in 'PS aux | grep php-fpm | grep-v grep | awk' {if ($4> = 1) print $2 }''
Do
Kill-9 $ PID
Echo $ PID> $ log
Done

You can add the script to crontab for regular execution.

Also, record several long-used commands:
1. list the processes with memory usage in order ???? Ps-A-sort-rss-o comm, pmem, pcpu | uniq-c | head-15
2. the command vmstat can monitor memory usage. as for memory monitoring, we are more concerned with swpd, free, si, and so. Generally, when the system is not busy, we can see that the value of swpd and so does not continue to be very high and is often 0.
3. view the memory usage details of a process through pmap, for example, pmap process number.

For more information, see workshop // www.opsers.org/linux-home/server/php-fpm-on-analysis-of-system-resources.html.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.