如何修复Magento中与load()方法相关的内存泄漏?

I'm importing data(products, customers, and orders) from a Magento store to another Magento store.

For example, when I try to import the products, I need to load the product to check if it already exists and use some attributes of the loaded product. I'm using the method Mage::getModel('catalog/product')->load($sku) and I run the script using command line.

Something like: $ php -f shell/mymodule.php

The problem is that Magento doesn't stop to increase the memory usage when I use the load() method in a loop.

foreach ($result['items'] as $item) {
     echo $index . ' - Memory: ' . memory_get_usage() . "
";

     /** @var Mage_Catalog_Model_Product $product */
     $product = Mage::getModel('catalog/product');
     $product->load($product->getIdBySku($item['sku']));

     $product->getOptionInstance()->unsetOptions()->clearInstance();
     unset($product);
     gc_collect_cycles();

     $index++;
}
  • 1 - Memory: 28147376
  • 2 - Memory: 34600320
  • 3 - Memory: 34661976
  • 4 - Memory: 34721128
  • 5 - Memory: 34776600
  • ...
  • 10 - Memory: 35051216
  • ...
  • 100 - Memory: 40148904

In some cases I need to import thousands of products, which causes a memory overflow. As you can see in the script above, I also tried to run some optimization functions within the foreach but that isn't enough in performance to prevent memory overflow.

$product->getOptionInstance()->unsetOptions()->clearInstance();
unset($product);
gc_collect_cycles();

I found a kind of solution that changes the Magento core, but it was made for Magento 1.4 and doesn't work for Magento 1.9 that I am using.

https://ringsdorff.net/2009/07/23/guest-post-fix-for-memory-leaks-in-magento/

Is there any effective solution to prevent the increase of memory usage in Magento 1.9?

duankuangxie9070
duankuangxie9070 循环内的加载导致内存用于每次迭代按集合大小递增,直到完成或内存耗尽,以先到者为准。magento.stackexchange.com/questions/69488/...
4 年多之前 回复
dtf1111
dtf1111 尝试检查magentowalk()函数的工作原理。它是一个通过集合的迭代器。你可能会发现一些线索如何避免这种情况。
4 年多之前 回复

2个回答



您可以使用Mage :: getSingleton()方法,它会将内存问题减少50%并尝试执行数据 块。</ p>
</ div>

展开原文

原文

You can use Mage::getSingleton() method, it will reduce the memory issue by 50% and also try to execute the data in chunks.



尝试 Mage :: getSingleton('catalog / product'); </ code>而不是 Mage :: getModel('catalog / product'); </ code> </ p>
</ div>

展开原文

原文

try Mage::getSingleton('catalog/product'); instead of Mage::getModel('catalog/product');

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐