Garbage Collection in Python

By: Python Documentation Team Emailed: 1605 times Printed: 2075 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The details of Python memory management depend on the implementation. The standard C implementation of Python uses reference counting to detect inaccessible objects, and another mechanism to collect reference cycles, periodically executing a cycle detection algorithm which looks for inaccessible cycles and deletes the objects involved. The gc module provides functions to perform a garbage collection, obtain debugging statistics, and tune the collector’s parameters.

Jython relies on the Java runtime so the JVM’s garbage collector is used. This difference can cause some subtle porting problems if your Python code depends on the behavior of the reference counting implementation.

In the absence of circularities, Python programs do not need to manage memory explicitly.

Why doesn’t Python use a more traditional garbage collection scheme? For one thing, this is not a C standard feature and hence it’s not portable. (Yes, we know about the Boehm GC library. It has bits of assembler code for most common platforms, not for all of them, and although it is mostly transparent, it isn’t completely transparent; patches are required to get Python to work with it.)

Traditional GC also becomes a problem when Python is embedded into other applications. While in a standalone Python it’s fine to replace the standard malloc() and free() with versions provided by the GC library, an application embedding Python may want to have its own substitute for malloc() and free(), and may not want Python’s. Right now, Python works with anything that implements malloc() and free() properly.

In Jython, the following code (which is fine in CPython) will probably run out of file descriptors long before it runs out of memory:

for file in very_long_list_of_files:
    f = open(file)
    c = f.read(1)


Using the current reference counting and destructor scheme, each new assignment to f closes the previous file. Using GC, this is not guaranteed. If you want to write code that will work with any Python implementation, you should explicitly close the file or use the with statement; this will work regardless of GC:

for file in very_long_list_of_files:
    with open(file) as f:
        c = f.read(1)


Python Home | All Python Tutorials | Latest Python Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Python Documentation Team
with in Python
goto in Python
Garbage Collection in Python
switch case statement in Python
Overloading in python
Static in python
Delegation in python
Schwartzian Transform in python
Multidimensional list (array) in python
Array in python
Remove duplicates from a list in python
Iterate over a sequence in reverse order in python
Convert between tuples and lists in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert number to string in python

More Tutorials in Python
What is the need for Python language?
How to compile python script and create .pyc file?
How to find the current module name in python
How to force rereading of a changed module in python
UnboundLocalError in python
call by reference in python
Callable objects in python
Ternary operator in python
Hexadecimal and Octal integers in python
Convert string to number in python
Convert number to string in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Convert between tuples and lists in python
Iterate over a sequence in reverse order in python
Remove duplicates from a list in python

More Latest News
Most Viewed Articles (in Python )
Iterate over a sequence in reverse order in python
Garbage Collection in Python
Installing gedit for python programming in Windows
Installing gedit for python programming in Linux
Your First Program in Python
Comments And Pound Characters in Python
Numbers And Math in Python
Variables And Names in Python
Variables And Printing in Python
Strings And Text in Python
Printing in Python
Formatted printing in Python
Using Unicode Literals in Python
Unknown command: 'migrate'
error: â_mysql_ConnectionObjectâ has no member named
Most Emailed Articles (in Python)
Numbers And Math in Python
call by reference in python
Schwartzian Transform in python
Unknown command: 'migrate'
What is the need for Python language?
Hexadecimal and Octal integers in python
Convert string to number in python
Installing gedit for python programming in Linux
Comments And Pound Characters in Python
Printing in Python
How to compile python script and create .pyc file?
How to force rereading of a changed module in python
UnboundLocalError in python
Callable objects in python
Multidimensional list (array) in python