for循环中的Magento内存泄漏
Magento memory leak in for loop
我不知道我所有的记忆都去了哪里,记忆在大约 28,000 次循环后耗尽。尝试了几种 unset 组合都无济于事。
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;
}
};
您可以在文件顶部添加以增加内存和时间限制。
set_time_limit(0);
ini_set('memory_limit','512M');
我不知道我所有的记忆都去了哪里,记忆在大约 28,000 次循环后耗尽。尝试了几种 unset 组合都无济于事。
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;
}
};
您可以在文件顶部添加以增加内存和时间限制。
set_time_limit(0);
ini_set('memory_limit','512M');