Avoid PHP-FPM memory leaks leading to memory exhaustion

Source: Internet
Author: User
Tags configuration php fpm


For PHP-FPM multi-process mode, it is simple to avoid a memory leak, which is to allow php-cgi to exit after processing a certain number of processes.
Otherwise, a memory leak caused by a PHP program or a third-party module, such as a imagemagick extension, can result in memory exhaustion or inadequacy.
Php-fpm.conf in the relevant configuration:
#请自行按需求配置

Pm.max_requests = 1024

There is actually another value associated with it, Max_children, which is how many processes are established each time php-fpm, so that the actual memory consumption is max_children*max_requests* each request uses memory.

Other rough methods include setting up a cron kill that consumes too much memory, php-cgi


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

In general, if the php-cgi process takes up more than 1% of the memory, consider whether or not to kill it. As a general rule, the php-cgi process typically consumes 0.2% or less.

Here is a script for you to use, that is, in a cron task, executed every minute.

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

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

The kill_php_cgi.sh script is as follows

#!/bin/sh
#如果是要杀掉php-FPM Process, 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;
Do
Kill-9 $pid
Done

Fi

2. Add memory and set the value of Php_fcgi_max_requests to the value that corresponds to your total memory storage

3. Optimize the program to reduce the amount of memory consumed per request

If PHP-FPM can provide configuration child process memory more than the specified size will be killed, then it is much more convenient.

The above is the cloud Community small series for your carefully prepared content, in the cloud Habitat Community Blog, quiz, public number, people, courses and other related content, welcome to continue to use the search button in the upper right corner to search memory, programs, processes, memory leaks, configuration php-fpm php fpm memory leak, how to avoid memory leaks Leakage, c avoid memory leaks, avoid memory leaks, information leaks can be caused, so that you get more relevant knowledge.

Avoid PHP-FPM memory leaks leading to memory exhaustion

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.