Memory_module_DDRAM_20-03-2006

Magento memory leak fix 1.4.x

A memory leak was found and identified in Mageto CE versions 1.4.x and has since been fixed in Magento CE versions 1.5.x. This should help speed up imports and inventory management, if you are currently using Community Edition 1.4.x.

Index: app/code/core/Mage/CatalogInventory/Model/Observer.php
===================================================================
--- app/code/core/Mage/CatalogInventory/Model/Observer.php	(revision 88134)
+++ app/code/core/Mage/CatalogInventory/Model/Observer.php	(working copy)
@@ -44,6 +44,14 @@
     protected $_itemsForReindex = array();
     /**
+     * Array, indexed by product's id to contain stockItems of already loaded products
+     * Some kind of singleton for product's stock item
+     *
+     * @var array
+     */
+    protected $_stockItemsArray = array();
+
+    /**
      * Add stock information to product
      *
      * @param   Varien_Event_Observer $observer
@@ -53,7 +61,12 @@
     {
         $product = $observer->getEvent()->getProduct();
         if ($product instanceof Mage_Catalog_Model_Product) {
-            Mage::getModel('cataloginventory/stock_item')->assignProduct($product);
+            $productId = intval($product->getId());
+            if (!isset($this->_stockItemsArray[$productId])) {
+                $this->_stockItemsArray[$productId] = Mage::getModel('cataloginventory/stock_item');
+            }
+            $productStockItem = $this->_stockItemsArray[$productId];
+            $productStockItem->assignProduct($product);
         }
         return $this;
     }

Published by

B00MER

What’s in a name other than a label attached to the things we’ve created and done for ubiquity... What is Molotov.bliss?

3 thoughts on “Magento memory leak fix 1.4.x”

Comments are closed.