Welcome, guest | Sign In | My Account | Store | Cart

You now that there is garbage in your program but you don't know what exactly. In this piece of code additionaly to the normal debugging output of gc the object itself are shown to get an idea where the leak may be.

Python, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import gc

def dump_garbage():
    """
    show us what's the garbage about
    """
        
    # force collection
    print "\nGARBAGE:"
    gc.collect()

    print "\nGARBAGE OBJECTS:"
    for x in gc.garbage:
        s = str(x)
        if len(s) > 80: s = s[:80]
        print type(x),"\n  ", s

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

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

    # show the dirt ;-)
    dump_garbage()
  

Situations that lead to the need of garbage collection are often to be avoided. Most of the time some objects refer to themselves or similar. But espacially in big programms you have to get an idea of where to find the leak and for this occasion it's good to know what the objects contain.