[Tutor] garbage collecting

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Jan 8 22:22:50 CET 2014


On Jan 8, 2014 9:11 PM, "Keith Winston" <keithwins at gmail.com> wrote:
>
>
> On Wed, Jan 8, 2014 at 3:30 PM, Oscar Benjamin <oscar.j.benjamin at gmail.com>
wrote:
>>
>> The garbage collector has nothing to do with the memory usage of
immutable types like ints. There are deallocated instantly when the last
reference you hold is cleared (in CPython). So if you run out of memory
because of them then it is because you're keeping them alive in your own
code. Running the garbage collector with gc.collect cannot help with that.
>
>
> Well that's sort of interesting... so it's different for mutables? huh.
Anyway, I think I got what I can reasonably hope to get from this question,
thanks to you and everyone!
>

It's not that it's different for mutables specifically. The primary
deallocation method is by reference count. This fails if there are
reference cycles so CPython has a cyclic garbage collector that deals with
those. Reference cycles can only emerge because of mutable containers.

A slight correction to what I wrote above is that if an immutable object
such as an int is referenced by an mutable one that is itself in a
reference cycle then it wouldn't be deallocated until the cyclic garbage
collector runs.

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140108/dcabbe09/attachment-0001.html>


More information about the Tutor mailing list