Reindexing Customer Grid Runs out of Memory in Magento 2

Published: September 10, 2018

Tags:

The 2.1.X release line of Magento current contains an issue where, with enough customers, reindexing the customer grid will fail with an out of memory fatal error

$ bin/magento indexer:reindex customer_grid
PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 280224918230723 bytes) in /var/www/html/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php on line 228

The issue is fixed in the in the 2.2.X release line and can be fixed in 2.1.X by applying PATCH_MDVA-4538.

Magento fixed this issue changing the source for Customer Grid <indexer>

diff --git a/vendor/magento/module-customer/etc/indexer.xml b/vendor/magento/module-customer/etc/indexer.xml
index b48592c..5f64442 100644
--- a/vendor/magento/module-customer/etc/indexer.xml
+++ b/vendor/magento/module-customer/etc/indexer.xml
@@ -11,7 +11,7 @@
         <title translate="true">Customer Grid</title>
         <description translate="true">Rebuild Customer grid index</description>
 
-        <fieldset name="customer" source="Magento\Customer\Model\ResourceModel\Customer\Collection"
+        <fieldset name="customer" source="Magento\Customer\Model\Indexer\Source"

The new source implements pagination in the getIterator method…

public function getIterator()
{
    $this->customerCollection->setPageSize($this->batchSize);
    $lastPage = $this->customerCollection->getLastPageNumber();
    $pageNumber = 0;
    do {
        $this->customerCollection->clear();
        $this->customerCollection->setCurPage($pageNumber);
        foreach ($this->customerCollection->getItems() as $key => $value) {
            yield $key => $value;
        }
        $pageNumber++;
    } while ($pageNumber <= $lastPage);
}

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.