[code.view]

[top] / python / PyMOTW / gc / gc_collect_with_del.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Show the objects with references to a given object.
     """
     #end_pymotw_header
     
     import gc
     import pprint
     
     class Graph(object):
         def __init__(self, name):
             self.name = name
             self.next = None
         def set_next(self, next):
             print '%s.next = %s' % (self, next)
             self.next = next
         def __repr__(self):
             return '%s(%s)' % (self.__class__.__name__, self.name)
         def __del__(self):
             print '%s.__del__()' % self
     
     # Construct a graph cycle
     one = Graph('one')
     two = Graph('two')
     three = Graph('three')
     one.set_next(two)
     two.set_next(three)
     three.set_next(one)
     
     # Remove references to the graph nodes in this module's namespace
     one = two = three = None
     
     # Show the effect of garbage collection
     print 'Collecting...'
     n = gc.collect()
     print 'Unreachable objects:', n
     print 'Remaining Garbage:', 
     pprint.pprint(gc.garbage)
         
     

[top] / python / PyMOTW / gc / gc_collect_with_del.py

contact | logmethods.com