magento2: possible memory leak in getUsedProducts function for configurables

Preconditions and environment

Steps to reproduce

Create a lot of configurable products with at least 80 variations by color and size

Load the products via productRepository using searchCriteria

Then in a foreach loop

Try to use the function

$this->configurableType->getUsedProducts($product)

You will end up seeing a blackfire profile like this where each execution of the function runs more slowly over time.

This is an example of 50 products in my case,

The first execution of the loop took 300ms, image

the final one took 5+sec image

Example code

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Api\SearchCriteriaBuilder;

class ConfigurableProductProcessor
{
    protected $productRepository;
    protected $configurableType;
    protected $searchCriteriaBuilder;
    
    public function __construct(
        ProductRepositoryInterface $productRepository,
        Configurable $configurableType,
        SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        $this->productRepository = $productRepository;
        $this->configurableType = $configurableType;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }
    
    public function processConfigurableProducts()
    {
        // Build the search criteria
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('type_id', 'configurable') // Filter by configurable products only
            // Add more filters, sorting, etc. if needed
            ->create();

        // Load configurable products based on the search criteria
        $configurableProducts = $this->productRepository->getList($searchCriteria)->getItems();
        
        foreach ($configurableProducts as $product) {
            $usedProducts = $this->configurableType->getUsedProducts($product);
            foreach ($usedProducts as $usedProduct) {
                // Process each used product
                // ...
            }
        }
    }
}

Expected result

Memory and time spent to fetch the products doesn’t increase per iteration.

Actual result

Time execution increases steadily and so does memory consumption

Additional information

I’m developing an XML export plugin for Magento that aims to export a vast catalog of products. The catalog comprises approximately 1 million products, with 28,000 of them being configurable products with multiple variations. However, I am encountering challenges due to the sheer volume of products, leading to memory exhaustion and a significant increase in processing time due to the magento core functions.

Unfortunately as it is it’s impossible to process the catalog properly.

My example was a limited example of 50 products. Imagine what happens with 28.000

I’ve provided the blackfire report to showcase that only magento core functions are taking up the majority of the slowness.

image

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

Hello @ioweb-gr,

Thanks for the contribution and collaboration!

We have tried to reproduce the issue and it seems the issue is reproducible in the Magento 2.4-develop branch. We have used memprof for issue reproduction.

Please find below the screenshot of qcachegrind UI for the reference:

image

Hence confirming the issue.

Thanks