Python Garbage Collection Flashcards
(45 cards)
What is the primary purpose of a garbage collector in Python?
To automatically manage memory by reclaiming memory occupied by objects that are no longer in use.
True or False: Python uses reference counting as its primary garbage collection mechanism.
True
Fill in the blank: The process of identifying and freeing memory occupied by objects that are no longer referenced is called _______.
garbage collection
What is reference counting?
A technique where each object keeps track of the number of references pointing to it.
What happens when an object’s reference count reaches zero?
The memory occupied by the object is reclaimed by the garbage collector.
Which module in Python provides an interface to the garbage collection facilities?
The ‘gc’ module.
True or False: Cyclic references can be handled by Python’s garbage collector.
True
What is a cyclic reference?
A situation where two or more objects reference each other, creating a cycle that prevents reference counting from reaching zero.
How does Python’s garbage collector handle cyclic references?
It uses a technique called cycle detection to identify and collect objects involved in cycles.
What is the function used to manually trigger garbage collection in Python?
gc.collect()
Fill in the blank: The default garbage collection strategy in Python is based on _______ and _______.
reference counting; cycle detection
What is the ‘gc.set_debug()’ function used for?
To enable debugging options for the garbage collector.
What does the ‘gc.get_objects()’ function return?
A list of all objects tracked by the garbage collector.
True or False: Garbage collection can lead to performance issues if not managed properly.
True
What is the purpose of the ‘gc.collect()’ method?
To force a garbage collection of unreachable objects.
What does the ‘gc.get_stats()’ function provide?
Statistics about the current state of the garbage collector.
What is ‘finalization’ in the context of garbage collection?
The process of calling an object’s finalizer method before the object is reclaimed.
True or False: Python’s garbage collector can be disabled.
True
What is the command to disable Python’s garbage collector?
gc.disable()
What is the impact of disabling the garbage collector?
It can lead to increased memory usage and potential memory leaks.
What is the purpose of weak references in Python?
To allow the garbage collector to reclaim an object without preventing its memory from being freed.
What module provides support for weak references?
The ‘weakref’ module.
Fill in the blank: Objects created with weak references can still be _______ by the garbage collector.
collected
What is a ‘weak reference’?
A reference to an object that does not increase its reference count.