4

I can't seem to find a definitive answer for this one.

When invoking a shell command using exec() from PHP, does the memory that shell command uses count towards the memory limit that the PHP script is given?

I realise that if the command generates a lot of output, and that output is captured in the $ouput (second) parameter of exec(), then that returned data could blow the PHP memory limit. However, assuming all output is sent to a file, if the exec() command requires 128M of memory to run, should a PHP script with a limit of 64M of memory be able to run it?

<?php
exec('command_using_128M_memory >/dev/null 2>&1');

I'm assuming PHP5.3+

1 Answer 1

7

The exec() command does not directly count towards the PHP since the process is executed separately not from within the PHP Process. Having said that if you use any output variable with the command they would count towards the limit.

So if you had a file which produced a lot of verbose logging, and you were capturing that logging it would count towards the memory limit.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I suspected this may be the case, but needed to know for sure before relying on it. I guess PHP keeps track of memory that it directly allocates to a script, but does not care what memory any child processes request for themselves.
I'm not so sure this is correct. I have PHP running out of memory using exec() and not specifying a return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.