Avoid memory depletion due to PHP-FPM Memory leakage-PHP source code

Source: Internet
Author: User
PHP-FPM if there is a storage leakage problem may cause insufficient server memory and thus cause the server resources to use up, this will cause the website to be unable to access, next let's take a look at a configuration to avoid Memory depletion caused by PHP-FPM Memory leakage. PHP-FPM if there is a storage leakage problem may cause insufficient server memory and thus cause the server resources to use up, this will cause the website to be unable to access, next let's take a look at a configuration to avoid Memory depletion caused by PHP-FPM Memory leakage.

Script ec (2); script

For PHP-FPM multi-process mode, to avoid Memory leakage problem is very simple, is to let the PHP-CGI after processing a certain number of processes to exit.
Otherwise, memory leaks caused by PHP programs or third-party modules (such as Imagemagick extensions) may cause memory depletion or insufficiency.
Php-fpm.conf with related configuration:
# Configure as needed

Pm. max_requests = 1024

In fact, there is another value associated with it max_children, which is how many processes will be created each time php-fpm, so the actual memory consumption is max_children * max_requests * memory usage for each request.

Some other brute-force methods include setting up cron to kill the php-cgi that occupies too much memory,


1. Check the memory usage of the php process and kill the process with excessive memory usage.

Generally, if the php-cgi process occupies more than 1% of the memory, you have to consider whether to kill it. In normal cases, php-cgi processes generally occupy 0.2% or less.

Here we provide a script for you to use, that is, to run it once every minute in a cron task.

Run 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
# If you want to kill the php-fpm process, change php-cgi to php-fpm in the following statement.
Pids = 'ps-ef | grep php-cgi | grep-v "grep" | grep-v "$0" | awk '{print $2 }''
If ["$ pids "! = ""]; Then
For pid in $ pids;
Do
Kill-9 $ pid
Done

Fi

2. Increase the memory and set the value of PHP_FCGI_MAX_REQUESTS to the value corresponding to the total memory storage.

3. Optimize the program to reduce the memory used for processing each request

If the PHP-FPM can provide the configuration that the sub-process memory exceeds the specified size and is killed, it is much easier.

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.