1

So I tried executing a script two different ways:

1)

foreach($result_array as $arg){  
    exec("/usr/bin/php pathToScript firstArg $arg", $array);                
    echo "peak usage: " . memory_get_peak_usage() . "\n\r";  
}

results:
peak usage: 5457324
peak usage: 7791212
PHP Fatal error: Allowed memory size of 33554432

2)

    foreach($result_array as $arg){
        curl_file_get_contents("website?query=$arg"); //just a cURL helper function
        echo "peak usage: " . memory_get_peak_usage() . "\n\r";
}

results:
peak usage: 5241708
peak usage: 5241708
peak usage: 5241708
peak usage: 5241708
peak usage: 5241708
peak usage: 5241708
... you get the idea

I must be mistaken about either the way exec() uses memory, or operates. It was my impression that when the program is forked, using exec(), that the calling script's memory requirements wouldn't be effected... However, this seems to not be the case.

Can anyone shed some light on what is going on here so I know what's going on?

0

2 Answers 2

1

The CURL version isn't saving the response (the output of curl_file_get_contents), but the exec version is by appending the contents to exec's second parameter of $array:

https://www.php.net/manual/en/function.exec.php

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

What happens is every response gets appended to the same array, ballooning the memory usage of the program.

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

Comments

1

The curl request is probably doing a full-blown HTTP request, so the script being requested is being run as a child of some completely independent webserver process. The memory usage of that child PHP process will be counted against the HTTP process handling the curl request, not your script.

1 Comment

Yes, this is clear... but my question isn't really about cURL, its about exec() and whether or not a script called by exec() increases the memory allotment of the calling script (which I previously didn't think was possible). I think I misunderstand how exec() manages memory, and I would like someone to straighten me out.

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.