I am trying to improve my understanding of how memory management works in python and I am confused about the concept of generations in pythons garbage collector module.
My understanding is as follows:
All objects created start in generation 0, once a threshold (700 by default for gen 0) is reached python will run a collection on that generation and any surviving objects age into the next gen.
Given the above, I cant understand the below output.
import gc
import sys
x = 1
print(gc.get_count())
gc.collect()
print(gc.get_count())
Output
(64, 1, 1)
(0, 0, 0)
Firstly, I've only run 1 line of code and Ive already got objects in gen 1 and 2 implying that garbage collection has already occurred at-least twice, how is this possible? Is there anyway to find out what objects are in each generation?
Secondly,
Why do I have 0 references in all generations after collection? I can still run the command print(x) and not get an error. Doesn't this mean there is still a reference to x and so it should exist in one of the generations?