views:

2022

answers:

3

Hi,

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.

+1  A: 

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
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
I've updated my answer somewhat - xdebug can be got/looked at from http://www.xdebug.org/
Meep3D
+1  A: 

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
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
adding the boolean true as the first parameter in memory_get_peak_usage(true) will return the memory for the PHP process, too.
scotts
+1  A: 

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