Linux – PHP eats up all the memory

linuxmemoryPHP

Since this morning one of our webserver's memory getting filled up by php which eventually going to cause the server to start swapping, slow down and become unavailable (an ssh login takes like 5 minutes then sometimes it dies in completely)…

I wonder how would you go on with solving these type of issues.

Jul 23 06:16:31 websrv2 kernel: [10409040.981882] php-cgi invoked oom-killer: gfp_mask=0x200da, order=0, oom_adj=0
Jul 23 06:16:31 websrv2 kernel: [10409040.981888] php-cgi cpuset=/ mems_allowed=0
Jul 23 06:16:31 websrv2 kernel: [10409040.981892] Pid: 27697, comm: php-cgi Not tainted 2.6.32-5-amd64 #1
Jul 23 06:16:31 websrv2 kernel: [10409040.981894] Call Trace:
Jul 23 06:16:31 websrv2 kernel: [10409040.981903]  [<ffffffff810b6714>] ? oom_kill_process+0x7f/0x23f
Jul 23 06:16:31 websrv2 kernel: [10409040.981908]  [<ffffffff8106bdee>] ? timekeeping_get_ns+0xe/0x2e
Jul 23 06:16:31 websrv2 kernel: [10409040.981912]  [<ffffffff810b6c38>] ? __out_of_memory+0x12a/0x141
Jul 23 06:16:31 websrv2 kernel: [10409040.981916]  [<ffffffff810b6d8f>] ? out_of_memory+0x140/0x172
Jul 23 06:16:31 websrv2 kernel: [10409040.981921]  [<ffffffff810baaf4>] ? __alloc_pages_nodemask+0x4ec/0x5fc
Jul 23 06:16:31 websrv2 kernel: [10409040.981927]  [<ffffffff810d92d8>] ? read_swap_cache_async+0x5d/0xf3
Jul 23 06:16:31 websrv2 kernel: [10409040.981931]  [<ffffffff810d93c5>] ? swapin_readahead+0x57/0x98
Jul 23 06:16:31 websrv2 kernel: [10409040.981937]  [<ffffffff8100c18d>] ? __raw_callee_save_xen_pte_val+0x11/0x1e
Jul 23 06:16:31 websrv2 kernel: [10409040.981941]  [<ffffffff810cd245>] ? handle_mm_fault+0x47f/0x80f
Jul 23 06:16:31 websrv2 kernel: [10409040.981947]  [<ffffffff813001a6>] ? do_page_fault+0x2e0/0x2fc
Jul 23 06:16:31 websrv2 kernel: [10409040.981952]  [<ffffffff812fe045>] ? page_fault+0x25/0x30
Jul 23 06:16:31 websrv2 kernel: [10409040.981954] Mem-Info:

I try to go the way to sort the processes by memory usage:

ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS

I see bunch of /usr/bin/php-cgi consuming memory. How can I limit the overall memory consumption by PHP so the server never gets pushed to the limit to use up all memory/swap and become unavailable?

More system info:

ii  php5                                5.3.3-7+squeeze18            server-side, HTML-embedded scripting language (metapackage)
ii  apache2                             2.2.16-6+squeeze11           Apache HTTP Server metapackage

Best Answer

you can limit the memory usage of a user (the user which runs the php-cgi processes) with cgroups.

for example to limit the memory usage of a user you can do the following:

# file: /etc/cgconfig.conf
group php {
    memory {
        memory.limit_in_bytes = "2G";
    }
}

And then you have to add your php-cgi user to this cgroup-group:

# file: /etc/cgrules.conf
phpcgiuser   memory   php

If you want to limit the memory usage per process you can do this with ulimit (100 MB per process).

# file: /etc/security/limits.conf
phpcgiuser  hard  as   102400

Edit: Nevertheless, you should analyze the software what it causing the memory leak.

Related Topic