You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Object lifetime and inner recursive function #49171
Since g calls "itself" in its own scope, it is stored as one of its own
cell vars, which creates a reference cycle. a is also part of its
reference cycle for the same reason, so it must wait for garbage
collection to be reclaimed.
If g didn't keep a reference to its cell vars, closures wouldn't be
possible, because the cell vars wouldn't survive the end of f's scope.
(g doesn't have to be recursive, it's enough that it makes a reference
to itself in its own scope:
deff():
a=A()
defg():
ag
or even:
deff():
a=A()
defg():
ahh=g
)
ocean-city commented on Jan 12, 2009
ocean-citymannequin
MannequinAuthor
More actions
Thank you for explanation. The combination of inner function + method
variable was very handy for me, but maybe I should refrain from using
it lightly. :-(
A little followup. Attached script q112.py (some puzzle program) ate up
my RAM (80MB) and never ran GC while solving. Current python GC looks
at the number of GC objects, but GC object like set object can contain
many non GC object, so... I feel it would be nice if there is better GC
triger/timing.
Well, tracking memory consumption of each container would be better than
simpling couting them, but it's much more complicated as well (not to
mention that memory consumption can vary, so you must recalculate it
periodically...).
eric-wieser commented on Feb 19, 2018
eric-wiesermannequin
Mannequin
More actions
Would it be possible for function self-reference cell vars to be weak references?
This wouldn't solve the issue for co-recursive inner functions, but would at least prevent reference cycles for the more common case of simple recursive functions.
Activity
ocean-city commentedon Jan 12, 2009
Hello. Sorry if this is noise. I expected
__del__
out of function
__del__
out of function
__del__
out of function
on following code, but actually I got
out of function
out of function
out of function
__del__
__del__
__del__
Is this expected behavoir? (I believed `a' would be
freed after returned from f(), so I was suprised)
If I remove the comment of gc.collect(), the code works as expected.
///////////////////////////////
pitrou commentedon Jan 12, 2009
Since g calls "itself" in its own scope, it is stored as one of its own
cell vars, which creates a reference cycle. a is also part of its
reference cycle for the same reason, so it must wait for garbage
collection to be reclaimed.
If g didn't keep a reference to its cell vars, closures wouldn't be
possible, because the cell vars wouldn't survive the end of f's scope.
(g doesn't have to be recursive, it's enough that it makes a reference
to itself in its own scope:
or even:
)
ocean-city commentedon Jan 12, 2009
Thank you for explanation. The combination of inner function + method
variable was very handy for me, but maybe I should refrain from using
it lightly. :-(
ocean-city commentedon Jan 12, 2009
A little followup. Attached script q112.py (some puzzle program) ate up
my RAM (80MB) and never ran GC while solving. Current python GC looks
at the number of GC objects, but GC object like set object can contain
many non GC object, so... I feel it would be nice if there is better GC
triger/timing.
pitrou commentedon Jan 12, 2009
Well, tracking memory consumption of each container would be better than
simpling couting them, but it's much more complicated as well (not to
mention that memory consumption can vary, so you must recalculate it
periodically...).
eric-wieser commentedon Feb 19, 2018
Would it be possible for function self-reference cell vars to be weak references?
This wouldn't solve the issue for co-recursive inner functions, but would at least prevent reference cycles for the more common case of simple recursive functions.
eric-wieser commentedon Sep 11, 2018
For anyone doing archaeology - this came up on python-dev about a year after this issue was filed: https://mail.python.org/pipermail/python-dev/2009-December/094439.html