#!/usr/bin/env python import gc import math import random import cairo print 'gc.isenabled() = ', gc.isenabled() gc.set_debug(gc.DEBUG_LEAK) surface = cairo.PDFSurface("testsurfaceA.pdf", 1000, 1400) cr = cairo.Context(surface) while 1: x = random.uniform(0, 1000) y = random.uniform(0, 1400) angle = random.uniform(0, math.pi * 2) n = random.uniform(0, 1000) try: cr.set_source_rgb(0,0,0) cr.set_font_size(2.0) m = cr.get_font_matrix() # new Matrix object m.rotate(angle) cr.set_font_matrix(m) cr.move_to(x, y) cr.show_text("%0.2f" % n) except MemoryError: print "Mem error, aborting. Iteration #%d" % i break gc.collect() print 'len(gc.get_objects())', len(gc.get_objects()) #print 'gc.get_objects()', gc.get_objects() # gc.garbage - a list of uncollectable objects print 'len(gc.garbage) =', len(gc.garbage) print 'gc.garbage:', gc.garbage """ Interpreting the output ----------------------- if 'len(gc.get_objects()) increases on each loop there is a possible memory leak (?) if len(gc.garbage) increases on each loop there is a memory leak """