Skip to content

Memory leak only on HYDRATE_SINGLE_SCALAR #7748

Open
@ZhukV

Description

@ZhukV

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions