Monday, March 9, 2015

Python Garbage Collection - Python Variables - Python Tutorial

Video Will Be Posted March 10th in the morning sorry for the delay! Have obligations tonight and do not have time to do the video.


Python Garbage CollectionPython Garbage Collection


One of the nicest things about the Python programming language is the Python garbage collection feature which will monitor and free up memory automatically. If we had to manage memory in Python programs we would devote more time allocating memory in our programs then we would be writing the program. In this Python tutorial, we will have a close look at how Python manages memory with its garbage collection algorithm.


What is Garbage Collection?


Garbage collection is a process whereby a program runs some code to clean up unused memory. In previous Python tutorials, we have discussed that the blocks of memory in Python are designated as objects. When these objects are not required anymore than the garbage collection system jumps in and removes those objects to clear up memory for more objects. This is all automatic so we are not required to initiate the garbage collection system to free up memory.


How Does The Garbage Collection Work?


When an object is no longer used then the garbage collection algorithm will remove the object. This is where the reference counter becomes involved. If the object is referenced then the reference counter increments up by one and when the reference to that object is removed then the counter increments down.  When the objects reference counter is at zero(No References To The Object) then that object will be deleted. The reference counter method is great but there are cases where this method of removing objects doesn’t always work.


Reference Cycles


Sometimes the objects reference counter never goes to zero and there is a lack of physical reference to an object. This happens when an object reference itself and this is referred to as a reference cycle. In this case, Python relies on a schedule for garbage collection to run. Each time a reference is added or removed the system will count the occurrence and when the number of occurrences reaches a set number then the garbage collection will run and remove reference cycles.


#Example of a Reference Cycle

>>> a = []
>>> a.append(a)
>>> a
[[...]]

In the above example, the variable “a” is assigned to an empty list then we append “a” to itself. Now we have an object referencing itself and in this case the list objects reference counter would never go to zero.


When Will Garbage Collection Run?


Garbage collection runs when the number of references added and deleted hit a certain number. We can find this number by importing the garbage collection module which gives us the ability to control the garbage collection. Let’s take a look at when will the garbage collection runs.


#When Will Garbage Collection Run?

>>> import gc
>>> gc.get_threshold()
(700, 10, 10)

We see 700 that means when we add and subtract 700 references the garbage collector will run and remove reference cycles.


Now we have covered the Python Garbage Collection we understand how Python automatically handles memory.  There is a module called garbage collection which we briefly discussed in this tutorial. We will examine the garbage collection module in full in its own tutorial. If you have any questions please let us know by leaving a comment below.


 



No comments:

Post a Comment