• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

php-fpm: Avoiding "Cannot allocate memory" Error with better configuration

Marcus

Well-known member
#1
My php-fpm failed and I wonder why. My server does not have so much memory. Did php crash because it wanted to spawn more processes but could because there was no memory? Or did it crash because it could not spawn more processes (which I do not think)?
Code:
[11-Aug-2014 08:17:57] NOTICE: fpm is running, pid 3385
[11-Aug-2014 08:17:57] NOTICE: ready to handle connections
[11-Aug-2014 14:50:38] ERROR: fork() failed: Cannot allocate memory (12)
[11-Aug-2014 14:50:40] ERROR: fork() failed: Cannot allocate memory (12)
[11-Aug-2014 14:50:41] ERROR: fork() failed: Cannot allocate memory (12)
[11-Aug-2014 14:50:42] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 16 total children
[11-Aug-2014 14:50:43] ERROR: fork() failed: Cannot allocate memory (12)
[11-Aug-2014 14:50:45] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 0 idle, and 16 total children
[11-Aug-2014 14:50:45] ERROR: fork() failed: Cannot allocate memory (12)
[11-Aug-2014 14:50:49] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 16 total children
[11-Aug-2014 14:50:49] ERROR: fork() failed: Cannot allocate memory (12)
[11-Aug-2014 14:50:51] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 16 total children
With the php RAM limit of 128M and 16 "total children", that means that php tried to allocate 16*128M = 2G of memory? Or are only the 8 children who php wants to "spawn" are used at the initial phase of failure? Then php tried to allocate 0,5G of RAM and failed.

I changed the configuration as follow and I hope it will be more stable:
Code:
sed -i 's|pm.start_servers = 5|pm.start_servers = 4|' /etc/php-fpm-5.5.d/www.conf
sed -i 's|pm.max_children = 50|pm.max_children = 4|' /etc/php-fpm-5.5.d/www.conf
sed -i 's|pm.min_spare_servers = 5|pm.min_spare_servers = 4|' /etc/php-fpm-5.5.d/www.conf
sed -i 's|pm.max_spare_servers = 35|pm.max_spare_servers = 4|' /etc/php-fpm-5.5.d/www.conf

I found this to determine the limit setting of pm.max_children. Is it correct that this setting along with the setting for the maximum of RAM used by php [standard is 128 MB] limits all php-fpm usage of RAM? I wonder why php tries to spawn more and more children.


http://myshell.co.uk/index.php/adjusting-child-processes-for-php-fpm-nginx/

- the following command will help us to determine the memory used by each (PHP-FPM) child process:
ps -ylC php-fpm --sort:rss
The RSS column shows non-swapped physical memory usage by PHP-FPM processes in kilo Bytes.
On an average each PHP-FPM process took ~75MB of RAM on my machine.
Appropriate value for pm.max_children can be calculated as:
pm.max_children = Total RAM dedicated to the web server / Max child process size – in my case it was 85MB
The server has 8GB of RAM, so:
pm.max_children = 6144MB / 85MB = 72
And I get this from php itself: "pm.min_spare_servers and pm.max_spare_servers cannot be greater than pm.max_children"
 
Last edited: