Memory Management in Python

Understanding the concept of Memory Management is so important for any software developer.

Python is a widely used Programming Language across software development,website design etc.writing short code in Python it means that writing memory efficient code.

Python supports the Auto Dynamic Memory Allocation concept. We do not worry about memory when we require the memory ,python allocates the memory as much as required.

Garbage Collection:

It is the process when the interpreter frees the memory,which is not used for a long time then the Garbage collector frees memory and makes it available for the other object.

Let’s understand via Diagram and Program

Program 1

Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:30:28) [MSC v.1926 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> x=100
>>> print(id(x))
1369026016
>>> 

The value is assigned into the x and the reference which is used is 1369026016 ,let’s see the diagram.

garbage collection diagram

Program 2

Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:30:28) [MSC v.1926 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x=100
>>> y=x
>>> id(x)
1369026016
>>> id(y)
1369026016
>>> print(x,y)
100 100
>>>

Here variable ‘x’ and ‘y’ use the same reference.

garbage collection value diagram

Subscribe Now