PHP-FPM using too much memory

PHPphp-fpm

I have a VPS box that runs a few websites for me as well as some scripts and it seems like its running out of memory. It has 1GB total memory. It looks like php-fpm exhausts the memory. Before it was simply killing other processes (like mysql) to free memory.

The error was:

php fpm has invoked oom-killer

I solved that using this solution: php-fpm invokes oom-killer

Next I tried to configure various php-fpm settings with no luck. I also increased my swap file size from 1GB to 2GB (2x my physical memory). I am running a total of 10 small websites which don't get much traffic at all so I'm confused why I am running out of memory. Besides purchasing more memory for my box, is there anything else I can do?

At this point php-fpm seems to hog up the entire 1GB of memory and most of the swap, in some cases it stops me from accessing the VPS as I can't authenticate!

Running a:

service php-fpm restart

Instantly frees up almost all the memory on the box (ie 950/990 in use goes to 200/990 in use). So what are my options? I can also post relevant configuration files if needed.

Relevant specs:
OS: Centos 6.7 x86_64
RAM: 1GB
Swap: 2GB
php -version: PHP 5.5.35 (cli) (built: Apr 29 2016 09:07:44)
php-fpm -version: PHP 5.5.35 (fpm-fcgi) (built: Apr 29 2016 09:09:09)

Best Answer

(this should be a comment but its a bit long).

"I solved that using this solution" - unfortunately most of the opinions there are wrong.

php fpm has invoked oom-killer

This means that:

  1. your system is configured to overcommit memory. Memory overcommit is useful when you have to manage badly written, long-running processes written in object oriented languages (like Java) and for vm hosts running large numbers of inactive guests. Neither seems to apply here. Setting the overcommit_memory to 2 as suggested will disable overcommit - so WTF does Chris suggest setting the overcommit_ratio to 80% (default usually about 50%) when it is disabled???

  2. PHP-FPM asked for more memory and there was none left. That doesn't necessarily mean that PHP-FPM used up all the memory.

Your problem is that the config your are using does not fit in the box you are using. But describing how to configure Linux, PHP, MySQL and a webserver is somewhat off topic here.

That restarting the FPM process seems to resolve the issue suggests that the problem is PHP-FPM, but it's no smoking gun. Maybe each PHP process has an open database connection.

Since you've not posted you php-fpm config nor provided any details of your webserver, nor given any metrics about response time, this question can't be answered here.

Either spend a lot of time and effort learning how to measure your systems behavour and tune it, or buy more memory.

Related Topic