2
以下代码在循环中。每个循环将URL更改为新地址。我的问题是每次传球占用越来越多的记忆。PHP cUrl循环泄漏内存
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://site.ru/');
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, 'http://site.ru/');
curl_setopt($ch, CURLOPT_HEADER, false);
$html = new \DOMDocument();
$html->loadHTML(curl_exec($ch));
curl_close($ch);
$ch = null;
$xpath = new \DOMXPath($html);
$html = null;
foreach ($xpath->query('//*[@id="tree"]/li[position() > 5]') as $category) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xpath->query('./a', $category)->item(0)->nodeValue);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, 'http://site.ru/');
curl_setopt($ch, CURLOPT_HEADER, false);
$html = new \DOMDocument();
$html->loadHTML(curl_exec($ch));
curl_close($ch);
$ch = null;
// etc.
}
内存是2000 Mb。脚本执行时间〜2小时。 PHP版本5.4.4。 如何避免内存泄漏?谢谢!从互联网
你为什么不使用函数?为了良好的理解和练习 – 2014-11-01 14:05:31
它是例如。 – 2014-11-01 14:07:40
我试过'ini_set('memory_limit','2GB');'并且在PHP ** 5.6.0 **中获得了成功。什么是你的PHP版本? – mpyw 2014-11-01 15:33:21