Performance with and without the garbage collector

Laurent Pointal laurent.pointal at free.fr
Sat May 14 13:37:11 EDT 2016


Steven D'Aprano wrote:

> Just for kicks, I've been playing around with running code snippets with
> and without the garbage collector enabled, looking to see if it will make
> any obvious difference to performance.
> 
> So far, I haven't found any.
> 
> For instance, I tried:
> 
> a = [i**3 for i in range(2000000)]
> del a[:]
> 
> thinking that garbage collecting almost two million ints would surely show
> some performance difference, but it doesn't.
> 
> Is anyone able to demonstrate a replicable performance impact due to
> garbage collection?

As CPython objects are reference-counted, you may see the GC in action if 
you setup some cycles in your script (here, your two million ints are 
removed — except for some small ints which remain alive for internal 
optimization).

>>> a = [i**3 for i in range(2000000)]
>>> b = [a]
>>> a.append(b)

>>> del a    # a remain referenced in b list
>>> del b    # b remain referenced in a list

Now, how to test for performance impact…

A+
Laurent.




More information about the Python-list mailing list