As we discussed in the comments, I don’t think there is a way to access the generation lists directly from python, you can set some debug flags, in python2 you can use the following to report objects that may or may not be assembled:
import gc gc.set_debug(gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_COLLECTABLE | gc.DEBUG_OBJECTS )
In python3, using the following, you will get some generation output and information about collective and unclaimed objects:
import gc gc.set_debug(gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_COLLECTABLE | gc.DEBUG_STATS)
You get output, for example:
gc: collecting generation 2... gc: objects in each generation: 265 4454 0 gc: collectable <function 0x7fad67f77b70> gc: collectable <tuple 0x7fad67f6f710> gc: collectable <dict 0x7fad67f0e3c8> gc: collectable <type 0x285db78> gc: collectable <getset_descriptor 0x7fad67f095e8> gc: collectable <getset_descriptor 0x7fad67f09630> gc: collectable <tuple 0x7fad67f05b88> gc: done, 7 unreachable, 0 uncollectable, 0.0028s elapsed. gc: collecting generation 2...
In the event of leaks according to gc.DEBUG_SAVEALL during installation, all unreachable objects will be added to the garbage and not to the release. This can be useful for debugging a program leak:
import gc gc.set_debug(gc.DEBUG_SAVEALL)
In python3, you can also add a callback that starts when gc starts and ends, a simple example:
def f(phase, info): if phase == "start": print("starting garbage collection....") else: print("Finished garbage collection.... \n{}".format("".join(["{}: {}\n".format(*tup) for tup in info.items()]))) print("Unreachable objects: \n{}".format( "\n".join([str(garb) for garb in gc.garbage]))) print() gc.callbacks.append(f)
Combining gc.DEBUG_SAVEALL with a function will show you any unreachable objects that are not much different from setting DEBUG_COLLECTABLE or DEBUG_LEAK , but one example of adding callback .
Padraic cunningham
source share