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 also dabbles in Ruby and Go. Technical topics that interest me are monitoring, security and performance.

During the day I solve challenging technical problems at Something Digital where I mainly work with the Magento platform. I also blog about tech, work on open source and hunt for bugs.

If you'd like to get in touch with me the best way is on Twitter.