Latest |Kites |Pictures |Programming |Life
[filed under Programming]Python memory leak detector

Tags: Python, memory leak, leek, object, garbage collection, garbage collector, __del__, inspect, gc, DEBUG_LEAK

I have a nasty memory leak in my Python script which I can't find. For some random projects my script gobbles up 2gig of memory then quietly dies.

Here is my first attempt at cobbling together a leak detector:

import gc
import inspect

def dump_garbage():
    # force collection
    print "\nCollecting GARBAGE:"
    gc.collect()
    # prove they have been collected
    print "\nCollecting GARBAGE:"
    gc.collect()
   
    print "\nGARBAGE OBJECTS:"
    for x in gc.garbage:
        s = str(x)
        if len(s) > 80: s = "%s..." % s[:80]
       
        print "::", s
        print "        type:", type(x)
        print "   referrers:", len(gc.get_referrers(x))
        try:
            print "    is class:", inspect.isclass(type(x))
            print "      module:", inspect.getmodule(x)
           
            lines, line_num = inspect.getsourcelines(type(x))
            print "    line num:", line_num
            for l in lines:
                print "        line:", l.rstrip("\n")
        except:
            pass

        print

class tmp(object):
    def __init__(self):
        a = 0

if __name__=="__main__":
    import gc
    gc.enable()
    gc.set_debug(gc.DEBUG_LEAK)

    # make a leak
    l = [tmp()]
    l.append(l)
    del l

    dump_garbage()

When run it outputs:

Collecting GARBAGE:
gc: collectable <tmp 00BE1730>
gc: collectable <list 00BED788>

Collecting GARBAGE:

GARBAGE OBJECTS:
:: <__main__.tmp object at 0x00BE1730>
        type: <class '__main__.tmp'>
   referrers: 4
    is class: True
      module: <module '__main__' from 'C:\XXXXXX.py'>
    line num: 33
        line: class tmp(object):
        line:     def __init__(self):
        line:         a = 0

:: [<__main__.tmp object at 0x00BE1730>, [...]]
        type: <type 'list'>
   referrers: 4
    is class: True
      module: None

As you can see it tries to figure out where the object is defined in your code. This should give you some clues as to where the leak is happening. If you are still having problems try adding some debug comments to the classes as you create them, print the debug info in the dump_garbage() function.

This is based on this python memory leak detector at active state. I am learning all this as I go. I guess outputing this as a .csv would help. If you have any comments or improvements drop me a line in the comments below and I'll update the code.

These are some links and stuff to help you get started hunting down your memory leak:

Update:

A simple urllib2 memory leak test.

19th of August, 2008@10:38:37 AM
add a comment, permanent link to article

Comments

Check this if you are a human being. Thanks. (I'm trying to reduce my comment spam :-)

Comment

Server Grind [0.0023 seconds]