Performance with and without the garbage collector

Steven D'Aprano steve at pearwood.info
Sat May 14 23:20:24 EDT 2016


For some reason, this post from INADA Naoki shows up on the python-list
email archives, but not comp.lang.python, which is what I use.


INADA Naoki wrote at Sat May 14 10:32:12 EDT 2016:

> Mark and Sweep GC is triggered when many new **tracked** objects are
> created and not destroyed.
>
> Since integer doesn't have reference to another objects, it's not tracked
> object. So your sample code doesn't show difference of GC.
> You can see gc stats via gc.set_debug(gc.DEBUG_STATS).

Thanks for the explanation! So lists, tuples, sets etc would all be tracked?

Following the suggestion to use this code:


class Tree:
    def __init__(self, n):
        if n > 0:
            self.a = Tree(n-1)
            self.b = Tree(n-1)
        self.n = n


def main():
    for _ in range(10):
        Tree(16)



I can see a significant difference in performance. With the gc on, running
main() on my computer takes 11.2 seconds, with the gc off, it takes 7.5
seconds.

Nice, and thank you!



-- 
Steven




More information about the Python-list mailing list