By: Kareem in Python Tutorials on 2012-04-07
Garbage collection is an automatic memory management process in Python that frees up memory space that is no longer being used by the program. It is responsible for detecting and cleaning up objects that are no longer needed by the program, allowing the memory space to be reused.
Python uses a technique called reference counting to keep track of the memory usage of objects. Every object in Python has a reference count, which is the number of references to the object from other objects in the program. When an object's reference count drops to zero, it means that the object is no longer being used by the program and can be safely deleted from memory.
However, reference counting has a limitation - it can't detect and clean up objects that refer to each other in a circular way, creating a memory leak. To address this limitation, Python also uses a technique called cycle detection, which identifies and cleans up circular references.
Python's garbage collector runs automatically in the background, periodically scanning the program's memory space for objects with zero reference counts and objects with circular references. When it finds objects that are no longer needed, it deallocates the memory space used by those objects and adds the freed memory back to the pool of available memory.
In addition to the automatic garbage collection process, Python also provides the
gc module, which allows you to control and fine-tune the garbage collection process. The
gc module provides functions for enabling and disabling the garbage collector, setting the threshold for when the garbage collector runs, and other advanced options.
Overall, Python's garbage collection mechanism provides a convenient and efficient way to manage memory in Python programs, allowing developers to focus on writing code rather than worrying about memory management.
Here's an example of how Python's garbage collection works:
import gc # create a circular reference between two objects a = [1, 2] b = [3, 4] a.append(b) b.append(a) # delete references to the objects a = None b = None # run the garbage collector gc.collect() # check if the objects have been garbage collected print(gc.garbage)
In this example, we create a circular reference between two objects (
b) by appending each object to the other. Then we delete the references to the objects by setting
None. Finally, we run the garbage collector using the
gc.collect() function and check if the objects have been garbage collected by printing the contents of the
When we run this code, we can see that the
gc.garbage list is empty, which means that the circularly-referenced objects have been successfully garbage collected.
Note that in most cases, you don't need to manually run the garbage collector in Python, as it runs automatically in the background. This example is just for illustration purposes to show how the garbage collector works.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Python )
Latest Articles (in Python)
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews