使用 CLI 与 Curl 的 PHP 内存使用情况
所以我尝试用两种不同的方式执行脚本:
1)
foreach($result_array as $arg){
exec("/usr/bin/php pathToScript firstArg $arg", $array);
echo "peak usage: " . memory_get_peak_usage() . "\n\r";
}
结果:
高峰使用量:5457324
高峰使用量:7791212
PHP 致命错误:允许的内存大小为 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";
}
结果:
高峰使用量:5241708
高峰使用量:5241708
高峰使用量:5241708
高峰使用量:5241708
高峰使用量:5241708
高峰使用量:5241708
...你明白了,
我一定对 exec() 使用内存或操作的方式有误解。我的印象是,当使用 exec() 分叉程序时,调用脚本的内存要求不会受到影响......但是,情况似乎并非如此。
任何人都可以阐明这里发生的事情,以便我知道发生了什么事吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CURL 版本不保存响应(
curl_file_get_contents
的输出),但exec
版本是将内容附加到exec
的内容$array
的第二个参数:https://www.php.net/manual/en/function.exec.php
发生的情况是每个响应都被附加到同一个数组,从而增加了程序的内存使用量。
The CURL version isn't saving the response (the output of
curl_file_get_contents
), but theexec
version is by appending the contents toexec
's second parameter of$array
:https://www.php.net/manual/en/function.exec.php
What happens is every response gets appended to the same array, ballooning the memory usage of the program.
curl 请求可能正在执行完整的 HTTP 请求,因此所请求的脚本作为某个完全独立的 Web 服务器进程的子进程运行。该 PHP 子进程的内存使用量将根据处理curl 请求的 HTTP 进程(而不是您的脚本)进行计算。
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.