Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question
5  
You're much better off fixing the memory leak in the application than trying to limit PHP's memory usage at the OS level. –  John Jul 23 at 12:50
    
Not my code, of course I can let the client know, it's not sure when they will do anything about it. PHP's memory_limit only limit it to a single script. It's better to do a workaround than let the server die again. –  posttek Jul 23 at 13:04
    
And for using ulimit, you cannot cap memory usage at the user level, ulimit can do that but for a single process. Even with using per user limits in /etc/security/limits.conf, a user can use all memory by running multiple processes. –  posttek Jul 23 at 13:19
1  
For all of these reasons and more, you should either fix the memory leak, force the client to fix the memory leak, or refuse to host the application until the memory leak is fixed. Seriously. Fix the application. –  John Jul 23 at 13:22
    
How many parallel PHP processes is apache allowed to spawn? What is the average RSS size of a PHP worker? –  Felix Frank Jul 23 at 13:39

2 Answers 2

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.

share|improve this answer

If this is the result of regular use, you should check those PHP scripts for memory leak, infinite loops, and such scary issues.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.