Importing 10,000 products through the admin panel ended with Allowed memory size exhausted after about 3,000 products. The import ran for 40 minutes without completing. Fix time: 3 hours.
Symptoms
PHP Fatal error: Allowed memory size of 536870912 bytes exhausted- UI import breaks halfway through
- CLI import (
bin/magento import:run) errors after a few thousand records - Server RAM is not exhausted – problem is only in the PHP process
Cause
The import module loaded all products into memory before saving. With 10,000 products and many EAV attributes, each product used ~50KB in memory = ~500MB just for data. Additional Magento objects (registry, event system) multiplied memory usage 3x.
Solution
php -dmemory_limit=4G bin/magento import:run --entity=catalog_product --behavior=append -- import_file=/var/www/html/var/import/products.csv
<?php
class BatchImporter
{
private const BATCH_SIZE = 500;
public function import(string $csvPath): void
{
$handle = fopen($csvPath, 'r');
$header = fgetcsv($handle);
$batch = [];
while (($row = fgetcsv($handle)) !== false) {
$batch[] = array_combine($header, $row);
if (count($batch) >= self::BATCH_SIZE) {
$this->processBatch($batch);
$batch = [];
gc_collect_cycles();
}
}
if (!empty($batch)) $this->processBatch($batch);
fclose($handle);
}
}
Result
Import of 10,000 products: from 40 minutes (incomplete) to 8 minutes (completed). Memory usage: stable at 256MB throughout the import.
