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;
     }

Possibly Related Posts:


  • Sean

    Which version and what are the versions being compared in the diff file?

  • Anonymous

    I believe it would be 1.4.x of the Community Edition.

  • Sean

    Confirmed in 1.4.1.1 CE and the fix is applied in 1.5.0.0 alpha 2