Memory leak only on HYDRATE_SINGLE_SCALAR #6256

Open
opened 2026-01-22 15:29:38 +01:00 by admin · 4 comments
Owner

Originally created by @ZhukV on GitHub (Jun 19, 2019).

Originally assigned to: @ZhukV on GitHub.

Bug Report

Q A
BC Break ?
Version 2.6.3

Summary

Memory leak only on AbstractQuery::HYDRATE_SINGLE_SCALAR.

Current behavior

If we use AbstractQuery::HYDRATE_SINGLE_SCALAR we have a memory leak in application. But if we use other hydrations, then all OK.

How to reproduce

Code snippet for reproduce:

<?php

namespace Some;

use AntiFraud\Context\AntiFraud\Transaction\Model\Transaction;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\NoResultException;
use Ramsey\Uuid\Uuid;

include_once __DIR__.'/vendor/autoload.php';

$kernel = new \AppKernel('prod', false);
$kernel->boot();

$em = $kernel->getContainer()->get('doctrine.orm.default_entity_manager');

$hydrateMode = AbstractQuery::HYDRATE_SCALAR;

for ($i = 0; $i < 10000; $i++) {
    $query = $em->createQueryBuilder()
        ->select('1')
        ->from(Transaction::class, 't') // Please change to other you entity
        ->andWhere('t.externalId = :id') // Please change the filter for other condition
        ->setParameter('id', Uuid::uuid1())
        ->setMaxResults(1)
        ->getQuery();

    try {
        $query->getResult($hydrateMode);
    } catch (NoResultException $e) {
    }

    if (\is_int($i / 1000)) {
        $em->clear();
        print \sprintf('Memory usage %.2f', \memory_get_usage() / (1024 * 1024)).PHP_EOL;
    }
}

Expected behavior

If we use the HYDRATE_SCALAR, then all OK. Memory is not leak. Sample output:

root@bcb37f4d1660:/code# php t.php 
Memory usage 9.67
Memory usage 9.67
Memory usage 9.67
Memory usage 9.67
Memory usage 9.67
Memory usage 9.67
Memory usage 9.67
Memory usage 9.67
Memory usage 9.67
Memory usage 9.67
root@bcb37f4d1660:/code# 

But, if you change the $hydrationMode to HYDRATE_SINGLE_SCALAR we have a memory leak. Output:

root@bcb37f4d1660:/code# php t.php 
Memory usage 9.74
Memory usage 29.39
Memory usage 49.05

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 16384 bytes) in /code/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php on line 117

But, this situations have other difficulties:

  • If you remove parameters and conditions, all OK, memory do not leak.
  • If you remove only parameter and set conditions to ->andWhere('t.externalId = '.\random_int(0, PHP_INT_MAX)) application is failed with memory leak.

Note:

  • I try to remove all cache drivers from configuration of entity manager.
Originally created by @ZhukV on GitHub (Jun 19, 2019). Originally assigned to: @ZhukV on GitHub. ### Bug Report | Q | A |------------ | ------ | BC Break | ? | Version | 2.6.3 #### Summary Memory leak only on `AbstractQuery::HYDRATE_SINGLE_SCALAR`. #### Current behavior If we use `AbstractQuery::HYDRATE_SINGLE_SCALAR` we have a memory leak in application. But if we use other hydrations, then all OK. #### How to reproduce Code snippet for reproduce: ```php <?php namespace Some; use AntiFraud\Context\AntiFraud\Transaction\Model\Transaction; use Doctrine\ORM\AbstractQuery; use Doctrine\ORM\NoResultException; use Ramsey\Uuid\Uuid; include_once __DIR__.'/vendor/autoload.php'; $kernel = new \AppKernel('prod', false); $kernel->boot(); $em = $kernel->getContainer()->get('doctrine.orm.default_entity_manager'); $hydrateMode = AbstractQuery::HYDRATE_SCALAR; for ($i = 0; $i < 10000; $i++) { $query = $em->createQueryBuilder() ->select('1') ->from(Transaction::class, 't') // Please change to other you entity ->andWhere('t.externalId = :id') // Please change the filter for other condition ->setParameter('id', Uuid::uuid1()) ->setMaxResults(1) ->getQuery(); try { $query->getResult($hydrateMode); } catch (NoResultException $e) { } if (\is_int($i / 1000)) { $em->clear(); print \sprintf('Memory usage %.2f', \memory_get_usage() / (1024 * 1024)).PHP_EOL; } } ``` #### Expected behavior If we use the `HYDRATE_SCALAR`, then all OK. Memory is not leak. Sample output: ```text root@bcb37f4d1660:/code# php t.php Memory usage 9.67 Memory usage 9.67 Memory usage 9.67 Memory usage 9.67 Memory usage 9.67 Memory usage 9.67 Memory usage 9.67 Memory usage 9.67 Memory usage 9.67 Memory usage 9.67 root@bcb37f4d1660:/code# ``` But, if you change the `$hydrationMode` to `HYDRATE_SINGLE_SCALAR` we have a memory leak. Output: ```text root@bcb37f4d1660:/code# php t.php Memory usage 9.74 Memory usage 29.39 Memory usage 49.05 Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 16384 bytes) in /code/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php on line 117 ``` But, this situations have other difficulties: * If you remove parameters and conditions, all OK, memory do not leak. * If you remove only parameter and set conditions to `->andWhere('t.externalId = '.\random_int(0, PHP_INT_MAX))` application is failed with memory leak. Note: * I try to remove all cache drivers from configuration of entity manager.
admin added the BugMissing Tests labels 2026-01-22 15:29:38 +01:00
Author
Owner

@lcobucci commented on GitHub (Oct 2, 2019):

@ZhukV would you please try to reproduce this without any external code, preferably sending us a PR with a functional test case? That would amazing and help us in solving this issue 👍

@lcobucci commented on GitHub (Oct 2, 2019): @ZhukV would you please try to reproduce this without any external code, preferably sending us a PR with a functional test case? That would amazing and help us in solving this issue :+1:
Author
Owner

@lcobucci commented on GitHub (Oct 2, 2019):

Possible duplicate of https://github.com/doctrine/orm/issues/7649

@lcobucci commented on GitHub (Oct 2, 2019): Possible duplicate of https://github.com/doctrine/orm/issues/7649
Author
Owner

@flaushi commented on GitHub (May 6, 2021):

Whats your backend? Postgresql?

@flaushi commented on GitHub (May 6, 2021): Whats your backend? Postgresql?
Author
Owner

@programmador commented on GitHub (May 17, 2021):

Whats your backend? Postgresql?

As far as I remember @ZhukV encountered the problem while using MySQL.

@programmador commented on GitHub (May 17, 2021): > Whats your backend? Postgresql? As far as I remember @ZhukV encountered the problem while using MySQL.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6256