[Tutor] python memory management

zakaria zemmoura.khalil at gmail.com
Sat Sep 3 18:20:07 EDT 2016


Is there any practical usage of using reference cycling?


On Sat, 2016-09-03 at 14:56 +0100, Alan Gauld via Tutor wrote:
> 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.
> 


More information about the Tutor mailing list