Magento allowed memory size of exhausted error
As Magento developer you may face with allowed memory size of exhausted error quite often. Magento is a system that may need a lot of memory. Sometimes you will need some hundred mega- or even gigabyte of RAM. I show you how to react on this error message.
Magento allowed memory size of exhausted error
The source of this error is quite obvious: your script needs more RAM that it can get. If you run your shop on a shared host you get a fixed number of maximum RAM memory. Sometimes you can change it in a hoster backend, but there is always a maximum. If you are on a root server you may have more RAM, but a script that uses too much is not a good idea, also if you may have enough. If you want to get more PHP maximum memory, you can set this in you php.ini or .htaccess file with:
1 | php_value memory_limit 512M |
For most Magento shops, 512 MB is more as enough. If you get this error again, you should optimize your code.
Code optimizations
The main source of allowed memory size of exhausted error messages are far too big collections. If you load a collection of 10.000 or even 100.000 products to loop, you are not a clever developer. If you loop a big collection in Magento 1, you can do this in pages. A sample code to loop a very big collection in blocks of 1000 orders may look as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //loop through all orders and create data $collection = Mage::getModel("catalog/order")->getCollection(); //Add a page size to the result set. $collection->setPageSize(1000); //discover how many page the result will be. $pages = $collection->getLastPageNumber(); $currentPage = 1; do{ //Tell the collection which page to load. $collection->setCurPage($currentPage); $collection->load(); foreach ($collection as $order) { //do something here with one order } //make the collection unload the data in memory so it will pick up the next page when load() is called. $collection->clear(); $currentPage++; }while ($currentPage <= $pages); |
First you get your collection as already known. In the next step you need to set a page size. 1000 is a good start, but you may optimize it. It should be as high as possible, but it should not reach your memory limit. Next you loop pages. You can get get number of pages by getLastPageNumber(). Each cycle you count computed page till you reach maximum number of pages. Each cycle you set current page with setCurPage(pagenumber) an now load that collection. Load uses memory, so you should only load pages with a limited number and not the whole collection. Now you can loop this page from collection as you already know.
You can use this code for all other objects like products or customers too.
Conclusion
Allowed memory size of exhausted error are avoidable. Whenever you loop a collection that can be big (number of orders, products and customers are increasing over time), you should do this in pages. With a bit more code, your function is future save. There is no difference in memory consumption if you run that loop for 1.000 or 1.000.000 objects. The only drawback is a slightly higher CPU workload.
What do you think about this?