Describe 'Generational Garbage Collection' in Python and how it optimizes performance.
Python interview question for Advanced practice.
Answer
Python's GC categorizes objects into three generations based on how many collection cycles they have survived. Generation 0 is for new objects. When a collection happens and an object survives, it is promoted to Generation 1, and eventually to Generation 2. This optimizes performance because younger objects are much more likely to be short-lived, so the GC scans Generation 0 more frequently than Generations 1 and 2, reducing the total scanning work.
Explanation
Python uses three generations: 0, 1, and 2.