cron 作业内存不足的速度非常快


Cron job running out of memory very fast

我托管在 godaddy 共享主机上。 我的数据库有 500 多个条目,但随着时间的推移,我希望它会更高。 也许有数千个。 我将其作为 cron 作业运行,但仅 64 条记录后内存不足(33 megs)。 我无法更改最大内存,因为我使用的是共享主机。 我可以做些什么来减少内存使用量? 也许有什么方法可以在每次循环时清除内存?

include('simple_html_dom.php');
$html = new simple_html_dom();  
$price_query = mysql_query("SELECT * FROM prices");
while ($price_rows = mysql_fetch_assoc($price_query))
{
$vendor_id = $price_rows['vendor_id'];
$product_id = $price_rows['product_id'];
$product_page = $price_rows['product_page'];
$product_string = $price_rows['product_string'];
$product_array = $price_rows['product_array'];
if (urlOK($product_page))
    {
    $html = file_get_html($product_page);  
    $new_price = $html->find($product_string); 
    $new_price = preg_replace('/['$,]/', '', $new_price);
    $product_price = $new_price[$product_array];
    $product_price = strip_tags($product_price);
    $qry="UPDATE prices SET product_price='$product_price' WHERE product_id = '$product_id' AND vendor_id = '$vendor_id'";
    mysql_query($qry);
    }
else
    {
    $qry="UPDATE prices SET product_price='0.00' WHERE product_id = '$product_id' AND vendor_id = '$vendor_id'";
    mysql_query($qry);
    }
$vendor_id = null;
$product_id = null;
$product_page = null;
    $product_string = null;
    $product_array = null;
$qry = null;
$html = null;
}

这是 im 调用的 1 个函数:

function urlOk($url) 
{
    $headers = @get_headers($url);
    if($headers[0] == 'HTTP/1.1 200 OK') return true;
    else return false;
}
好的,

经过几个小时的研究,我已经弄清楚了这一点。 问题本身出在simple_html_dom库中。 如果在循环中使用,则会有大量内存泄漏。 要修复,只需将其添加到循环末尾:

$html->clear();
unset($html);