21

I have used valgrinds massif tool to monitor memory usage in the past.

Does anyone know how to capture memory use of php processes that are spawned on a linux lighttpd server?

I have found that Valgrind can not attach to a prerunning process (and I would not know the PID of the php process before hand anyway)

I only see lighttpd's memory usage, not the PHP processes that are spawned by lighttpd cgi module.

Thanks in advance.

DEzra
  • 2,978
  • 5
  • 31
  • 50

4 Answers4

16

PHP has it's own memory testing functions, I don't know if that's any use to you, but if you just want to log it you could use: http://php.net/manual/en/function.memory-get-peak-usage.php

    echo "Using ", memory_get_peak_usage(1), " bytes of ram.";
scragar
  • 6,764
  • 28
  • 36
  • 2
    Is this the memory usage for just the script, or does it include the php process memory use as well? Also, it will be hard to get the peak usage at any one moment in time if multiple scripts are running. – DEzra Jul 02 '09 at 08:11
  • 3
    adding the boolean true as the first parameter in memory_get_peak_usage(true) will return the memory for the PHP process, too. – scotts Mar 02 '10 at 00:11
9

Can't you use the 'ps' tool?

$ ps -F -C php-cgi

UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
http     10794 10786  0  4073   228   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
http     10795 10794  0  4073    28   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
http     10796 10786  0  4073   228   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
http     10797 10796  0  4613  3544   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
...

RSS is the Real-memory (resident set) size in kilobytes of the process.

To sum it all up in bash (a bit rusty sorry)

#!/bin/bash

total=0
for i in `ps -C php-cgi -o rss=`
do
    total=$(($total + $i))
done
echo "Memory usage: $total kb"

# Output: Memory usage: 4540 kb

One liner:

total=0; for i in `ps -C php-cgi -o rss=`; do total=$(($total+$i)); done; echo "Memory usage: $total kb";

I know the reliability of the memory part in ps is questioned but at least it gives you an idea of what the usage is like.

Palm
  • 359
  • 2
  • 5
  • This is good if you're using php-cgi, if you're using mod_php you're out of luck and probably want to go with the first answer. – John Hunt Oct 01 '12 at 10:52
4

Besides the build-in commands shown above, you can use XHProf for profiling your scripts and XHGui for showing profiling results in a nice browser application. You get in-depth information on how your methods use memory and what are the peaks of memory usage within your application.

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
  • Thanks, I alway like to hear about new tools for profiling. I will defintely check out XHProf and XHGUI – DEzra Feb 06 '13 at 21:12
2

http://php.net/manual/en/function.memory-get-usage.php

Should give you the amount of memory that the thread is using from within the script itself. I think because the script (and thread) only exists for a few milliseconds at most - just the time it takes to generate the page - catching it outside PHP might be difficult.

  • Plan B

You can also get debugging information from the server that may be more accurate - I use xdebug personally, and when it throws an error/notice it gives you a stack trace, time and memory usage. You can trigger it at the end of the script with:

trigger_error ('Finished', E_USER_NOTICE);

And it'll give you the info. I am not sure at catching the data - if you need to there may be a function in the docs on how - I vaguely remember seeing one.

Meep3D
  • 3,803
  • 4
  • 36
  • 55
  • 2
    hi, yes, I tried both memory_get_usage and memory_get_peak_usage on a script that created a string and looped 50 times increasing the string each time. It seems that the memory_get_usage() always returns 2mb (in bytes). Which seems wrong to me (unless php pre allocates 2mb for all scripts?? – DEzra Jul 02 '09 at 08:10
  • 1
    I've updated my answer somewhat - xdebug can be got/looked at from http://www.xdebug.org/ – Meep3D Jul 02 '09 at 08:48