doumei1772 2016-05-23 07:52
浏览 67
已采纳

如何修复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?

  • 写回答

2条回答

  • dongtuji0992 2016-05-24 17:42
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的