Python Garbage Collection

  • By Mayuresh Marale
  • February 24, 2023
  • Python
Python Garbage Collection

Python Garbage Collection

Introduction To Python Garbage Collection

Python Garbage Collection is the process of automatic deletion of unused objects to free memory. GC is a built-in technique used in programming languages for dynamic memory management.

In Python, developers need not manually allocate or de-allocate the memory, as it is managed internally and automatically by a garbage collector.

Highlights

  • How memory is managed in Python?
  • How Python are objects stored in memory?
  • What is garbage collection?

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Python is Dynamically typed

When we assign a value to a variable in Python, the type declaration of the variable is optional. It specifies the kind of variable used in runtime. Strict type declaration is required for variable assignment in other languages like Java, C, C++, etc. Looking for comprehensive and hands-on experience in Python Programming? Look no further than SevenMentor’s Python classes in Pune!

As shown below, we assign a value to the identifier and Python detects the type of object dynamically.

Python Garbage Collection

 

Python objects in memory

Python has names, not variables. Python object is stored in memory with names and references. A name is just a label for an object, hence object can have many names. Reference is a pointer that points to the memory address where the object is stored.

Python object comprised as follows :

  1. Type
  2. Value
  3. Reference count

While assigning a value to a name, Python automatically detects its type as shown above. Now value is stored at some memory location while defining the object. Then Reference is assigned to the name and Reference count contains count of names pointing that object.

Python Garbage Collection

 

Python Garbage Collection Memory Management through Automatic

Garbage collection is used free the memory by deleting the unused objects and respective memory slots are reused by mentioning them in free memory slots for storing new objects.

Python garbage collection is just like the recycling of memory in computers.

Python garbage collector starts running as soon as code execution starts.

 

How does it work?

Python uses two mechanisms for memory management: 

  • Reference counting
  • Garbage collection

 

  1. Reference Counting

Always references are counted and stored in memory with the value of the object. The reference count of an object changes when the number of aliases to the same object changes. Once a new name or alias is assigned to the object, its reference count increases, while it decreases when the object is deleted.

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Whenever the reference count of an object reaches 0, the garbage collector is activated, which dynamically and automatically collects it automatically, and shows the respective memory location from allocated spaces to free spaces, hence making unused memory slots available for use by the system for further allocation.

Scenario-1

We have assigned x to 50, then x to y, and finally z to 50. Even though we assigned new names, still value is same, hence all names refer to the same object, but the reference count increments by 1 for every new name. We compared objects with value comparison <==> and identity <is> operators to check whether objects and respective values are the same or different.

Python Garbage Collection

 

In continuation to the above example, now if we try to change the value of names x and y, by assigning values None and False respectively, new objects got created in memory.

Once we change x and y to None and False respectively, Python created new None and False objects,  old object is dereferenced, thus making its reference count from 3 to 1, since that previous object still has a reference by z, hence it is not deleted.

Python Garbage Collection

 

  1. Garbage Collection

Scenario-3

With del(z), before deleting z, the Python interpreter invokes the destructor of a class __del__(), which decreases the reference count to z by one, making it zero i.e. out-of-scope, hence this object is now eligible for the garbage collection. This del(z) statement doesn’t delete objects, it just removes the name (and reference) to the object.

Python Garbage Collection

 

Hence as and when the reference count of the object becomes zero i.e. out-of-scope, the instances of memory are no longer referenced by any object making them orphaned instances. The garbage collector now deletes the object and deallocates orphaned instances of memory making them reusable by the system for further allocations to other objects in the future. Join our Python training in Pune today and take the first step towards a rewarding career in software development or data science!

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Conclusion

  • Python Garbage Collector (GC) achieves memory management, for the same Python GC periodically, automatically, and dynamically reclaims, deletes, and frees memory blocks consumed by unused objects.
  • A garbage collector is considered similar to the memory manager of an operating system.
  • The primary purpose of garbage collection in Python is to reduce memory leaks and ensure memory recovery.
  • The garbage collection in Python is an additional process running in the background, it is overhead and hence hampers system performance.

 

Author:-

Mayuresh Marale
Call the Trainer and Book your free demo Class For Python

Call now!!! | SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*