mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Long running process leaks memory #6803
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 (Aug 7, 2021).
Bug Report
Summary
I think there is a memory leak in long running processes.
Current behavior
My memory consumption grows all the time although I keep no reference to to visited nodes and clear the em regularly.
I am traversing an object graph using iteration (not recursion). My stack only has the identifiers, not the entities.
How to reproduce
please see my example https://stackoverflow.com/questions/68686479/leaking-memory-while-traversing-an-object-graph/68686896#68686896 here
Expected behavior
I'd expect to get along with no more than a few megabyte memory consumption all the time.
@beberlei commented on GitHub (Aug 7, 2021):
You can use the mwmory profiler to find where this memory is https://github.com/arnaud-lb/php-memory-profiler
@flaushi commented on GitHub (Aug 7, 2021):
Wow, I didn't know about this tool, great!

However, this is the situation:
the query being executed is this:
this should just query the entities and add them to the UnitOfWork, which I clear every regularly. How is it possible that memory is leaked then?
Edit:
This is confusing. My code actually fetches many more entities, but like
Am I guessing correctly that memprof only reports allocations that have not been freed, so that the DQL query is the one which leaks??
Thank you so much for your help!
@greg0ire commented on GitHub (Aug 7, 2021):
From the description in the README (emphasis mine):
@flaushi commented on GitHub (Aug 7, 2021):
So, I am speechless. This means then that the Repository method leaks???
I thought when I load an entity through the entitiy manager it is inserted in the UnitofWork which is cleared properly by $em->clear().
I changed the repository method to first load only the ids of suitable entities an then
findthemagain here a new screenshot

so this looks as if the repository method has a leak? Where?
Or could the rest of my code be leaking?
for the sake of completeness:
@greg0ire commented on GitHub (Aug 7, 2021):
When you call the repository, since
clearisn't called inside it, more memory is used than before, presumably because of the entity map. Although this fits the definition of a leak, it is intended, but memprof doesn't know about this.Maybe you could try using https://github.com/BitOne/php-meminfo instead?
I think that instead of showing you what method "leaked" memory, it will show you what objects are taking up so much memory. There is even a guide on hunting down memory leaks:
https://github.com/BitOne/php-meminfo/blob/master/doc/hunting_down_memory_leaks.md
Hope this helps, I haven't had to do this myself before.
@flaushi commented on GitHub (Aug 7, 2021):
Thanks for this direction I will follow it tmorrow.
Anyway the fact that I am calling
$em->find(DataCategory::class, $id)over and over without seeing it in the memprof, but my DQL query withgetResult()being shown makes me wonder.To conclude this support case:
A) you are not aware of any memleak in queries and
getResult's, right?B) And it should be possible to "travel" the association graph of entities over millions of jumps without leaking memory, too? (of course with intermediate
$em->clear()'s)C) Both,
$em->find(fqcn, $id)and$em->createQuery()->getResult()are supposed to return entities that are stored automatically in the entityMap before being returned to me?Is there an option to get hydrated but unmanaged entities from the entity manager? (I guess no)
@nuryagdym commented on GitHub (May 2, 2022):
I guess it is the same problem describe here: https://stackoverflow.com/questions/26616861/memory-leak-when-executing-doctrine-query-in-loop
I am running $em->clear() periodically, and still have the memory leak issue.
so in symfony config/package/doctrine.yaml I have this option:
with
logging: falsedoctrine does not log queries into the log file but I guess doctrine is keeping logs somewhere in memory that is why I am having memory leak issue.So the solution is either
--no-debugoption$em->getConnection()->getConfiguration()->getSQLLogger(null);profiling: false:Otherwise, sql logger
Doctrine\DBAL\Logging\DebugStackis keeping all the queries