Use __del__ methods to improve gc?

Andrew Bennetts andrew-pythonlist at puzzling.org
Sun Jun 15 09:47:19 EDT 2003


On Fri, Jun 13, 2003 at 11:27:45AM -0500, Edward K. Ream wrote:
> > I suspect the paragraph you mean might be...: [snip]
> 
> That's the one.  Thanks Alex.  It is now up on my wall :-)
> 
> BTW, I'm chuckling a little less now.  Executing:
> 
> print len(gc.garbage), len(gc.get_objects())
> 
> periodically shows that my app is steadily eating memory.  len(gc.garbage)
> remains zero while len(gc.get_objects()) steadily increases.  And I thought
> I was recycling all bindings :-(
> 
> You wouldn't know of something similar to timeit for storage allocation,
> would you?

Something that's might help a little is:

    import gc, sys, types
    
    def mostRefs(n=30):
        d = {}
        for obj in gc.get_objects():
            if type(obj) in (types.ClassType, types.TypeType):
                d[obj] = sys.getrefcount(obj)
        counts = [(x[1],x[0]) for x in d.items()]
        counts.sort()
        counts = counts[-n:]
        counts.reverse()
        return counts

Which is a rather nasty way to find out what the classes and types have the
most instances alive (because each instance has a reference to its class, a
class's ref count is roughly equal to the number of instances).

-Andrew.






More information about the Python-list mailing list