Issue 9825: OrderedDict ref cycles cause memory leak (original) (raw)

Created on 2010-09-10 19:04 by jek, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
odicttest.py jek,2010-09-10 19:04 test script
Messages (6)
msg116036 - (view) Author: jason kirtland (jek) Date: 2010-09-10 19:04
Circular graphs of collections.OrderedDict are uncollectable due to the presence of OrderedDict.__del__. >>> from collections import OrderedDict >>> import gc >>> left, right = OrderedDict(), OrderedDict() >>> left['other'] = right >>> right['other'] = left >>> assert not gc.garbage >>> del left, right >>> gc.collect() 10 >>> assert not gc.garbage Traceback (most recent call last): File "", line 1, in AssertionError Not an issue in 3.1.1, have not verified with 3.1.2.
msg116039 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-09-10 19:38
This is an unfortunate aspect of using __del__. I don't see a way around it without reintroducing weak references. Of course, your code can also use weak ref proxies to avoid creating uncollectible circular garbage.
msg116041 - (view) Author: jason kirtland (jek) Date: 2010-09-10 20:19
I find the behavior surprising compared to dict and other containers, where this is not an issue and weakrefs are not required in user code. I would not be surprised, however, to have to wait for a gc.collect() to clean up my cycles like I do for regular objects. For what it's worth, the pure-python alternative linked in the docs uses neither __del__ nor weakrefs, though it is undoubtably less efficient than this implementation. And the workaround 'del OrderedDict.__del__' seems to work as well, if one is willing to wait for a gc collection.
msg116128 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-09-11 22:44
On #python-dev, Raymond, Amaury and Benjamin agreed that __del__ should be removed, with the rationale that this method should be used to release resources other than memory.
msg116135 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-09-11 23:58
Éric, I've got this one. Thx.
msg116147 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-09-12 04:22
For 2.7, removed __del__ in r84725. For 3.2, replaced __del__ with weakrefs in r84727.
History
Date User Action Args
2022-04-11 14:57:06 admin set github: 54034
2010-09-12 04:23:00 rhettinger set status: open -> closedresolution: fixedmessages: +
2010-09-11 23:58:42 rhettinger set messages: +
2010-09-11 22:44:30 eric.araujo set nosy: + eric.araujomessages: +
2010-09-10 20:19:12 jek set messages: +
2010-09-10 19:38:34 rhettinger set priority: normal -> lowmessages: +
2010-09-10 19:10:05 benjamin.peterson set assignee: rhettingernosy: + rhettinger
2010-09-10 19:04:02 jek create