Page 1 of 1

Excessive Resource Usage - Virtual Memory Size - php-cgi

Posted: 03 Nov 2017, 09:55
by Ryan_G
I am getting excessive resource usage emails for a script that runs on PHP using cron. Most of the time there are no warnings (even with the USERMEM threshold set as low as 10MB) but at least once per day I get emails for half an hour or so almost every time the script runs - these usually show Virtual Memory 300-400MB but it can be as high as 1GB.

The pertinent details of one of the emails are:

Resource: Virtual Memory Size
Exceeded: 848 > 800 (MB)
Executable: /path/to/php-cgi
Command Line: /path/to/myscript.php

To monitor the situation I put memory_get_peak_usage(true) at the end of the script (which records the maximum memory used by the script when it runs) and every time it is 2MB (even for the time when I get the warning).

These figures are obviously very different so how can this be explained? Is the CSF Virtual Memory figure correct?

Note that I know how to configure CSF to stop the emails but I want to find out if there genuinely is a problem with my script leaking memory or another genuine cause of this issue.

Re: Excessive Resource Usage - Virtual Memory Size - php-cgi

Posted: 03 Nov 2017, 10:04
by ForumAdmin
csf takes the information directly from the process in /proc/PID/. It's likely that the PHP script is reporting RSS memory usage (memory used in RAM) rather than VSZ (virtual memory size). These are checked by lfd using PT_USERRSS and PT_USERMEM respectively.

You would need to increase PT_USERMEM from its current setting of 800 to avoid the event being triggered.

Re: Excessive Resource Usage - Virtual Memory Size - php-cgi

Posted: 03 Nov 2017, 10:36
by Ryan_G
Thanks for the information. The PHP memory_get_peak_usage(true) reports the memory that is currently allocated to the PHP script (so I think this should be Virtual Memory - however it is only memory allocated in PHP's internal memory manager i.e. memory directly used by PHP variables etc).

So if CSF takes the information directly from the process in PID would it include memory used by things like CURL or MySQL that are called by the script but have memory allocation independent of PHP?

Thanks for your help - this could really help me track down a stubborn memory leak.

Re: Excessive Resource Usage - Virtual Memory Size - php-cgi

Posted: 06 Nov 2017, 09:25
by Ryan_G
I will reword the question in my last post to make it clearer in the hope I can get an answer here before having to contact support:

In the warning email the executable is php-cgi and the script is a script written in PHP which makes requests to a MySQL database and uses CURL to make http requests - will the memory used figure in the warning emails include memory used by MySQL and CURL or is it just memory used by PHP?

Re: Excessive Resource Usage - Virtual Memory Size - php-cgi

Posted: 06 Nov 2017, 10:24
by ForumAdmin
It reports the total memory used by the process as taken from /proc/PID/status from the VmSize entry.

Re: Excessive Resource Usage - Virtual Memory Size - php-cgi

Posted: 06 Nov 2017, 10:51
by Ryan_G
Does the total memory used by the process as taken from /proc/PID/status from the VmSize entry include memory used by MySQL and CURL or is it just memory used by PHP? (Note executable is shown as php-cgi)

Re: Excessive Resource Usage - Virtual Memory Size - php-cgi

Posted: 06 Nov 2017, 10:54
by ForumAdmin
It is the memory used by that process PID. What it includes depends on what the process uses during its lifetime, but won't include memory from any subprocesses or daemons it connects to.

Re: Excessive Resource Usage - Virtual Memory Size - php-cgi

Posted: 06 Nov 2017, 11:02
by Ryan_G
Perfect thanks - this really helps