【问题标题】:php curl memory usagephp curl 内存使用情况
【发布时间】:2013-01-19 04:24:14
【问题描述】:

我有这个从页面列表中获取 html 的函数,一旦我运行它 两个小时左右,脚本中断并显示已超出内存限制, 现在我尝试取消设置/设置为空一些变量,希望能释放一些内存 但这是同样的问题。各位大佬可以看一下下面的片子吗 代码? :

{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    if ($proxystatus == 'on'){
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_URL, $site);
    ob_start();
    return curl_exec($ch); // the line the script interrupts because of memory
    ob_end_clean();
    curl_close($ch);

    ob_flush();
    $site = null;
    $ch = null;

}

非常感谢任何建议。我已将内存限制设置为 128M,但之前 增加它(对我来说似乎不是最好的选择)我想知道是否有 在运行脚本时我可以做的任何事情来减少内存/释放内存。

谢谢。

【问题讨论】:

  • 您是否将其编码为类的方法并通过 CLI 运行它?
  • 不,它是一个函数,它通过一个 url 列表循环并获取 html。是的,我通过命令行使用它。

标签: php memory curl ob-start


【解决方案1】:

确定这不是 cURL 问题。使用 xdebug 之类的工具来检测脚本的哪一部分正在消耗内存。

顺便说一句,我还将它更改为不运行两个小时,我会将其移动到每分钟运行的 cronjob,检查它需要什么然后停止。

【讨论】:

    【解决方案2】:

    您确实在泄漏内存。请记住,return 会立即结束当前函数的执行,因此您的所有清理(最重要的是 ob_end_clean()curl_close())都不会被调用。

    return 应该是函数所做的最后一件事。

    【讨论】:

    • 谢谢。但是如果我更多地 curl_close() 和 ob_end_clean() 会有任何返回值吗??
    • 好吧,你必须将curl_exec的返回值保存到一个变量中,然后运行你的清理,然后return这个变量。
    • 我是 curl 的新手,但这会有所作为吗?我们将返回值分配给一个变量并清除该返回值,然后返回存储在变量中的相同值。无论如何,我这样做了,但我没有看到任何变化...... :(
    • 谢谢,我使用 xdebug 跟踪内存问题,现在一切正常。非常感谢。
    【解决方案3】:

    我知道这已经有一段时间了,但其他人可能会遇到类似的问题,所以如果它可以帮助其他人...... 对我来说,这里的问题是 curl 设置为将输出保存到字符串。 [curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 会发生这种情况] 如果输出过长,脚本将耗尽该字符串的允许内存。 [返回类似FATAL ERROR: Allowed memory size of 134217728 bytes exhausted (tried to allocate 130027520 bytes) 的错误] 解决此问题的方法是使用 curl 提供的其他输出方法之一:输出到标准输出,或输出到文件。无论哪种情况,都不需要 ob-start。

    因此,您可以使用以下任一选项替换大括号的内容:

    选项 1:输出到标准输出:

    $ch = curl_init();
    if ($proxystatus == 'on'){
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_URL, $site);
    curl_exec($ch);
    curl_close($ch);
    

    选项 2:输出到文件:

    $file = fopen("path_to_file", "w"); //place this outside the braces if you want to output the content of all iterations to the same file
    $ch = curl_init();
    if ($proxystatus == 'on'){
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
    }
    curl_setopt($curl, CURLOPT_FILE, $file);    
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_URL, $site);
    curl_exec($ch);
    curl_close($ch);
    fclose($file);  //place this outside of the braces if you want to output the content of all iterations to the same file
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 2012-09-20
      相关资源
      最近更新 更多