
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Garbage Collector interface (gc)
Automatic garbage collection is one of the important features of Python. Garbage collector mechanism attempts to reclaim memory occupied by objects that are no longer in use by the program.
Python uses reference counting mechanism for garbage collection. Python interpreter keeps count of number of times an object is referenced by other objects. When references to an object are removed, the count for an object is decremented. When the reference count becomes zero, the object memory is reclaimed.
Normally this mechanism is performed automatically. However, it can be done on purpose if a certain situation arises in the program. The 'gc' module defines the garbage collection interface. Following functions are defined in 'gc' module
enable() | Enable automatic garbage collection. |
disable() | Disable automatic garbage collection. |
isenabled() | Returns true if the automatic collection is enabled. |
collect() | With no arguments, run a full collection. The optional integer argument specifies which generation to collect(from 0 to 2). A ValueError is raised if the generation number is invalid. The number of unreachable objects found is returned. |
set_threshold() | Set the garbage collection thresholds (thecollection frequency). |
get_threshold() | Return the current collection thresholds as a tuple |
callbacks | A list of callbacks that will be invoked by the garbage collector before and after collection. |
- Related Articles
- How many ways to call garbage collector (GC) in Java?\n
- Garbage collection(GC) in JavaScript?
- How does Garbage Collector work in C#
- Do you think garbage collector can track all the Python objects?
- How can we call garbage collection (GC) explicitly in Java?
- Garbage Collection in Python
- Destroying Objects (Garbage Collection) in Python
- How does garbage collection work in Python?
- Python GNU readline Interface
- When are python objects candidates for garbage collection?
- What is garbage? What does garbage consist of?
- What is Pantograph Collector in Electric Traction?
- A garbage collector separate items mentioned below in the garbage into red, green and blue containers for their transfer to landfill, composting pit, and recycling unit respectively.Items:Plastic bagsNewspaper and journalsScrew and nutsVegetable peelsMetal chipsEgg shellsWhich item was transferred to which bin?
- When are python classes and class attributes garbage collected?
- Python Interface to Shell Pipelines

Advertisements