1

I'm seeing my long running php processes utilize significant amounts of memory, and it grows very quickly. After about a day, I saw the following:

# pmap <pid>  

000000000091c000    588K rw---  /usr/bin/php  
00000000009af000    108K rw---    [ anon ]  
00000000013ab000 256948K rw---    [ anon ]  
00007f9ed0000000    132K rw---    [ anon ]  
...  
00007f9edcaa6000      8K rw---  /usr/lib64/php/modules/curl.so  
00007f9edcaa8000 103580K r----  /usr/lib/locale/locale-archive  
...  
 total           629312K  

# cat /proc/<pid>/status  


Name:   php  
State:  S (sleeping)  
...  
VmHWM:    268920 kB  
VmRSS:    268920 kB  
VmData:   334368 kB  
VmStk:       136 kB  
VmExe:      3188 kB  
VmLib:     22752 kB  
VmPTE:       912 kB  
VmSwap:        0 kB  
Threads:    1  
...  
Mems_allowed:   00000000,00000001  
Mems_allowed_list:  0  
voluntary_ctxt_switches:    11902137



ps aux  


0.5  4.3 694124 333864 ?       S    Jan21   8:11 php  

top  

PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  CODE DATA COMMAND  
20   0  677m 326m 9100 S  0.0  4.4   8:11.56 3188 390m php  

but, memory_get_usage(true) consistently returns:
1835008

There seems to be a memory leak, but how can I diagnose the cause, and reduce it? I've tried to utilize tools such as this, but similar to memory_get_usage, it doesn't notice any additional memory usage

I've also tried: # strace -p -e trace=memory but all I see are brk() calls, like so:

brk(0)                                  = 0x14f6f000
brk(0x14f90000)                         = 0x14f90000

I'm on version 5.4.27:

# php --version
PHP 5.4.27 (cli) (built: Apr 23 2014 23:34:13)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
sinneduy
  • 127
  • 6
  • Xdebug doesn't have good support for long running scripts afaik, but: http://stackoverflow.com/questions/17648097/how-do-you-debug-a-long-running-php-script – Michael Berkowski Jan 22 '15 at 20:31
  • @MichaelBerkowski, it seems like that's very helpful for determining what code gets executed, but not necessarily in seeing where the memory usage is going. It should be noted that the memory-profiler I've linked to does a pretty good job of showing what's been executed as well. – sinneduy Jan 22 '15 at 20:38

1 Answers1

0

You can compile PHP on your own using the --enable-debug configure option. If you execute a php cli script compiled that way you'll get a detailed explanation about detected memory leaks - if there are any.

However, the fact that memory_get_usage() returns a constant value looks weird. Can you reduce the problem to a single script? (Or show your script?) Can you nail it down to certain PHP extension? Which version of PHP are you using?

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • added version to OP. However, I cannot show my script. The pmap implies to me that it isn't an extension, however – sinneduy Jan 22 '15 at 20:35
  • Is the leak reproducable? – hek2mgl Jan 22 '15 at 20:39
  • I would try to compile PHP with `--enable-debug`. (You can't file a bug report without providing a reproducable example) – hek2mgl Jan 22 '15 at 20:43
  • I'll look into it, but it seems like a bit of a long shot considering php isn't seeing additional memory usage, and you stated "...you'll get a detailed explanation about detected memory leaks - if there are any." – sinneduy Jan 22 '15 at 20:44
  • I'm not a C expert, especially not C dev-tools expert, but I expect them using valgrind - meaning the leaks aren't detected by PHP itself (which would make no sense).. I've used this feature during extension development and found it quite helpful. Also when using the debug version of PHP I detected memory leaks even in "regular" scripts. *regular* means scripts using PHP core functionality only. I found leaks not that much but also not that rarely. Therefore I would encourage you to do that too. (unless you can't provide a reproducable example)... – hek2mgl Jan 22 '15 at 20:49