#35279: Memory Leak with `prefetch_related`
-------------------------------------+-------------------------------------
Reporter: Ken Tong | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: memory leak | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Antoine Humbert):
The following code snippet shows the same result:
{{{
import gc
class Parent:
def __init__(self):
self.cache = {}
class Child:
def __init__(self, parent):
self.parent = parent
def test():
foo = Parent()
bar = Child(parent=foo)
foo.cache["bars"] = [bar]
print(foo.cache, bar.parent)
test()
gc.collect()
print(len(gc.garbage))
gc.set_debug(gc.DEBUG_LEAK)
gc.collect()
print(len(gc.garbage))
test()
gc.collect()
print(len(gc.garbage))
}}}
Results in following output
{{{
{'bars': [<__main__.Child object at 0x6f520cdd90>]} <__main__.Parent
object at 0x6f520cd6d0>
0
0
{'bars': [<__main__.Child object at 0x6f520b32d0>]} <__main__.Parent
object at 0x6f520b1fd0>
gc: collectable <Parent 0x6f520b1fd0>
gc: collectable <Child 0x6f520b32d0>
gc: collectable <list 0x6f520b1600>
gc: collectable <dict 0x6f520b1e80>
4
}}}
Removing the `gc.set_debug` statement, the `gc.garbage` is always empty,
so it looks like à side effect of `DEBUG_LEAK`.
As per the `gc` documentation:
{{{
To debug a leaking program call gc.set_debug(gc.DEBUG_LEAK). Notice that
this includes gc.DEBUG_SAVEALL, causing garbage-collected objects to be
saved in gc.garbage for inspection.
}}}
So, using `DEBUG_LEAK` leads to collected objects to be present in
gc.garbage. So, I would say that looking at `gc.garbage` in this case does
not identifies a memory leak. On the contrary, it shows objects that were
garbage collected
--
Ticket URL: <https://code.djangoproject.com/ticket/35279#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018e19d62d2e-5094eb02-7637-4d38-b9a5-90d10ac10694-000000%40eu-central-1.amazonses.com.