1. 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

Discussion in 'Server Configuration and Hosting' started by Marcus, Aug 11, 2014.

  1. Marcus

    Marcus Well-Known Member

    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/

    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: Aug 11, 2014
  2. Floren

    Floren Well-Known Member

Share This Page