for循环中的Magento内存泄漏

时间:2015-02-19 06:20:00

标签: php magento memory-leaks foreach

我无法弄清楚所有内存的去向,内存在大约28,000次循环后耗尽。尝试了几种未设置的组合无济于事。

updateProduct()仅被注释掉以帮助我找出问题。

任何帮助将不胜感激。

include_once '../app/Mage.php';

Mage::init();
//fetch all products
$res = Mage::getSingleton('core/resource');
$db = $res->getConnection('catalog_read');
$productTable = $res->getTableName('catalog/product');
$sql = $db->select()->from($productTable, 
    'entity_id'
);
//Returns about 162,000 rows
$rows = $db->fetchAll($sql);
$rowcount = count($rows);

$progress = 0;

foreach($rows as $productRow)
    {
    $product_id = $productRow["entity_id"];
    if($product_id){
        //memory leaks here.
        $product=Mage::getModel('catalog/product')->setStoreID(0)->load($product_id);
        //updateProduct($product);
    }else{
        echo "LOAD ERROR\n";
    }   

    $product->clearInstance();
    unset($product); 
    gc_collect_cycles();
    //echo memory_get_usage() . " 49\n";

    if($progress % 500 == 0)
          {
            echo "$progress out of $rowcount complete\n";
            echo memory_get_usage() . "\n";
          }
    $progress++;
    }

updateProduct匹配某些分层导航属性的折扣:

function updateProduct($product_id)
    {
    global $off10,$off20,$off30,$off40,$off50;
    //echo "$product_id\n";
    $product=Mage::getModel('catalog/product')->setStoreID(0)->load($product_id);
    $attribute = $product->getResource()->getAttribute('discount');

    if ($attribute)
        {
        $discount_value += $attribute->getFrontend()->getValue($product);
        //Match discount to layered_nav_discount and all attributes < discount
        if($discount_value < 10){
            $value = "";
        }elseif($discount_value < 20){
            $value = "$off10";
        }elseif($discount_value < 30){
            $value = "$off10,$off20";
        }elseif($discount_value < 40){
            $value = "$off10,$off20,$off30";
        }elseif($discount_value < 50){
            $value = "$off10,$off20,$off30,$off40";
        }elseif($discount_value >= 50){
            $value = "$off10,$off20,$off30,$off40,$off50";
        }
        //Update layered_nav_discount
        //$_product->addAttributeUpdate('layered_nav_discount', $value, 0);

        unset($product);
        return;
        }
    };

1 个答案:

答案 0 :(得分:0)

您可以添加文件顶部以增加内存和时间限制。

set_time_limit(0);
ini_set('memory_limit','512M');
相关问题