Question

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.

Was it helpful?

Solution

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.";

OTHER TIPS

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top