mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Possible mem leak in Query::getSingleScalarResult #6202
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @flaushi on GitHub (Mar 20, 2019).
Originally assigned to: @lcobucci on GitHub.
Bug Report
My DQL query selects a number from an entity:
This query is executed tens of thousands of times inside a Symfony console application.
Now if I use
getSingleScalarResultthe memory consumption will explode.However, switching to
getResulthelps:In the latter case, memory consumption stays bearable.
I found this issue and developed a TestCase.
Unfortunately the test case does not produce the memory leak.
However, when repeating the queries, memory consumption does increase. This should not be the case IMHO?
gives
One has to say that I had to switch to PHP 7.2 for the test case to compile and also pulled orm from master branch. My software uses PHP 7.1 and ORM 2.6.2
@Ocramius commented on GitHub (Mar 20, 2019):
Is the memory usage progression coming from a symfony context? Can it be reproduced without symfony (since the test case seems to deny that)?
Maybe this is related to query logging?
@flaushi commented on GitHub (Mar 20, 2019):
As I pointed out, in my fuctional test case, memory does explode, too. However there it does not make a difference if I use
getResultorgetSingleScalarResult.In my production code, I am only exchanging
getSingleScalarResultwithgetResultin the repository code. In first case, 1G of memory is exhausted after 1700 iterations. WithgetResult, all 17000 iterations run with peak mem usage of 480 MB.@flaushi commented on GitHub (Mar 20, 2019):
https://gist.github.com/flaushi/03b18152dea60bc2dba1ff405140b820
Run this test case with
gives:
@Ocramius commented on GitHub (Mar 20, 2019):
I'm referring to this bit:
Can the test be adapted to reproduce the problem?
@Ocramius commented on GitHub (Mar 20, 2019):
Oh, posted at the same time. Lemme check that test case then 👍
@Ocramius commented on GitHub (Mar 20, 2019):
I just went through this, and found out that the
OrmFunctionalTestCasealready leaks (due to static connection and SQL logger).This is the rewritten test, which in my case doesn't leak:
Produces:
Note how I use
memory_get_usage(), notmemory_get_peak_usage(), since PHPUnit also adds overhead.Can you check if the modified test is flawed or not?
@flaushi commented on GitHub (Mar 20, 2019):
I can confirm that the modified test does not leak memory.
Still, I definitely see the described behavior in my console application (no debug, no sqlLogger and no second level cache logger).
Definitely, the one and only difference is my repository method:
@flaushi commented on GitHub (Mar 20, 2019):
I also wrapped the line
in a try-catch to check if this produces the leak, but no.
@Ocramius commented on GitHub (Mar 20, 2019):
Since we can exclude ORM from being the root cause here, I think you should investigate and see what the ORM configuration (
EntityManager#getConfiguration()andConnection#getConfiguration()) yield. Possibly a framework dependency collecting things over time.@flaushi commented on GitHub (Mar 20, 2019):
Yeah I also thought that's it but couldn't find anything.
I am irritated about the fact that
getSingleScalarResultis the reproducible trigger...@Ocramius commented on GitHub (Mar 20, 2019):
Maybe this is related to some listeners?
@flaushi commented on GitHub (Mar 20, 2019):
I do have listeners, but only pre/posPersist/Update/Flush. This command does not issue any
$em->flush()orpersistso none of them should be called.I'll try a MySQL connection now...
@flaushi commented on GitHub (Mar 20, 2019):
I can't believe it. It seems to be the PostgreSQL connection.
This explains why the TestCase did work: it's a SQLite in-memory db.
I uploaded a 100 % equal data set to a Postgres and MySQL connection.
On MySQL the memory consumption is the same for both
getResultandgetSingleScalarResult,on Postgresql the
getSingleScalarResultversion has increasing memory consumption while thegetResultis clean. Incredible.@lcobucci commented on GitHub (Oct 2, 2019):
@flaushi @Ocramius shall we close this one?
@flaushi commented on GitHub (Oct 2, 2019):
Yes, I did not re-encounter the problem since then. Thanks for your support!