[Tutor] python memory management

Alan Gauld alan.gauld at yahoo.co.uk
Sat Sep 3 09:56:25 EDT 2016


On 03/09/16 04:25, monikajg at netzero.net wrote:

> Is this what you mean? 
> a = 5
> b = a
> a = b

No, you are confusing variable names with objects.
Here you only have one object - the number 5.
For a cycle you need at least 2 objects and those
objects must be able to reference another object.
In practice that means a collection or an instance
of a class.

a = []
b = [a]
a.append(b)

The two lists are now cyclically referring to each
other. We can now delete the names a and b and
the two list objects will continue to exist
in memory even though no variables refer to them.
This is where the second garbage collector comes
into play, it can recognise the link between the
lists and the fact that no variable refers to them.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list