Memory Management And Garbage Collection In Python

Memory Management And Garbage Collection In Python

·

3 min read

Python is a Dynamically Typed Language.

We don’t declare the type of a variable when we assign a value to the variable in Python. It states the kind of variable in the runtime of the program. Other languages like C, C++, Java, etc.., there is a strict declaration of variables before assigning values to them.

As you can see below, we just assign a variable to an object and Python detects the type of the object.

1 sJhfhKJhCE__j_1pwPzRSA.png Python detects type pf objects dynamically

How are Python objects stored in memory?

In C, C++, and Java we have variables and objects. Python has names, not variables. A Python object is stored in memory with names and references. A name is just a label for an object, so one object can have many names. A reference is a name(pointer) that refers to an object.

Every Python object has 3 things.

Python objects have three things: Type, value, and reference count. When we assign a name to a variable, its type is automatically detected by Python as we mentioned above. Value is declared while defining the object. Reference count is the number of names pointing that object.

1 sJhfhKJhCE__j_1pwPzRSA.png Every Python objects has three things

Garbage Collection:

Garbage collection is to release memory when the object is no longer in use. This system destroys the unused object and reuses its memory slot for new objects. You can imagine this as a recycling system in computers.

Python has an automated garbage collection. It has an algorithm to deallocate objects which are no longer needed. Python has two ways to delete the unused objects from the memory.

1.Reference counting:

The references are always counted and stored in memory.

In the example below, we assign c to 50. Even if we assign a new variable, the object is the same, the reference count increases by 1! Because every object has its own ID, we print the IDs of objects to see if they are the same or different.

1 sJhfhKJhCE__j_1pwPzRSA.png When we change the value of a like in below, we create a new object. Now, a points to 60, b and c point to 50.

When we change a to None, we create a none object. Now the previous integer object has no reference, it is deleted by the garbage collection.

We assign b to a boolean object. The previous integer object is not deleted because it still has a reference by c.

1 sJhfhKJhCE__j_1pwPzRSA.png Now we delete c. We decrease the reference count to c by one.

1 sJhfhKJhCE__j_1pwPzRSA.png As you can see above, del() statement doesn’t delete objects, it removes the name (and reference) to the object. When the reference count is zero, the object is deleted from the system by the garbage collection.

Conclusion:

Python is a high-level language and we don’t have to do the memory management manually. Python garbage collection algorithm is very useful to open up space in the memory. Garbage collection is implemented in Python in two ways: reference counting and generational. When the reference count of an object reaches 0, reference counting garbage collection algorithm cleans up the object immediately. If you have a cycle, reference count doesn’t reach zero, you wait for the generational garbage collection algorithm to run and clean the object. While a programmer doesn’t have to think about garbage collection in Python, it can be useful to understand what is happening under the hood.