Avoid PHP-FPM memory leaks resulting in memory exhaustion

Source: Internet
Author: User
Tags fpm

For PHP-FPM multiple process mode, to avoid the memory leak problem is very simple, is to let php-cgi processing a certain number of processes after the exit.
Otherwise, memory leaks caused by PHP programs or Third-party modules (such as ImageMagick extensions) can lead to memory exhaustion or inadequacy.
There are related configurations in php-fpm.conf:
#请自行按需求配置

Pm.max_requests = 1024

There is actually another value associated with it Max_children, this is how many processes each php-fpm will build, so the actual memory consumption is max_children*max_requests* each request using memory.

Other rough ways include setting up cron kill-php-cgi that consume too much memory,


1. Check the memory footprint of the PHP process, kill the memory using the excess process

In general, if the php-cgi process consumes more than 1% of the memory, consider whether to kill it. Because of normal circumstances, the php-cgi process typically occupies 0.2% or less.

Here's a script for you to use, just put it on a cron task and run it every minute.

Use the CRONTAB-E command, and then add the following scheduling task

* * * * */bin/bash/usr/local/script/kill_php_cgi.sh * * *

The kill_php_cgi.sh script is as follows

#!/bin/sh
#如果是要杀掉php-FPM process, in the following statement php-cgi please change to PHP-FPM
pids= ' Ps-ef|grep php-cgi|grep-v "grep" |grep-v "$" | awk ' {print $} '
If ["$pids"!= ""];then
for PID in $pids;
Todo
Kill-9 $pid
Done

Fi

2. Increase the memory and set the value of the php_fcgi_max_requests to the value corresponding to your total memory storage amount

3. Optimize the program, reduce the amount of memory used to process each request

If PHP-FPM is able to provide configuration subprocess memory exceeding the specified size, it will be more convenient.

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.