Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak only on HYDRATE_SINGLE_SCALAR #7748

Open
ZhukV opened this issue Jun 19, 2019 · 4 comments
Open

Memory leak only on HYDRATE_SINGLE_SCALAR #7748

ZhukV opened this issue Jun 19, 2019 · 4 comments
Assignees

Comments

@ZhukV
Copy link

ZhukV commented Jun 19, 2019

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.
@lcobucci
Copy link
Member

lcobucci commented 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
Copy link
Member

lcobucci commented Oct 2, 2019

Possible duplicate of #7649

@flaushi
Copy link
Contributor

flaushi commented May 6, 2021

Whats your backend? Postgresql?

@programmador
Copy link

Whats your backend? Postgresql?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants