def memory_dump():
    dump = open('memory.pickle', 'w')
for obj in gc.get_objects():
    i = id(obj)
size = sys.getsizeof(obj, 0)
referents = [id(o) for o in gc.get_referents(obj) if hasattr(o, '__class__')]
if hasattr(obj, '__class__'):
    cls = str(obj.__class__)
cPickle.dump({'id': i, 'class': cls, 'size': size, 'referents': referents},
    dump)
How do I find what is using memory in a Python process in a production system?