How to fix memory leaks in Magento related to load() method?
<p>I'm importing data(products, customers, and orders) from a Magento store to another Magento store.</p>

<p>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.</p>

<p>Something like:
    $ php -f shell/mymodule.php</p>

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

<pre><code>foreach ($result['items'] as $item) {
     echo $index . ' - Memory: ' . memory_get_usage() . "\n";

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

     $product-&gt;getOptionInstance()-&gt;unsetOptions()-&gt;clearInstance();
     unset($product);
     gc_collect_cycles();

     $index++;
}
</code></pre>

<ul>
<li>1 - Memory: 28147376</li>
<li>2 - Memory: 34600320</li>
<li>3 - Memory: 34661976</li>
<li>4 - Memory: 34721128</li>
<li>5 - Memory: 34776600</li>
<li>...</li>
<li>10 - Memory: 35051216</li>
<li>...</li>
<li>100 - Memory: 40148904</li>
</ul>

<p>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.</p>

<pre><code>$product-&gt;getOptionInstance()-&gt;unsetOptions()-&gt;clearInstance();
unset($product);
gc_collect_cycles();
</code></pre>

<p>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.</p>

<p><a href="https://ringsdorff.net/2009/07/23/guest-post-fix-for-memory-leaks-in-magento/" rel="nofollow">https://ringsdorff.net/2009/07/23/guest-post-fix-for-memory-leaks-in-magento/</a></p>

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