Memory Management in Python

Memory Management in Python

4 mins read1.2K Views Comment
Updated on Aug 24, 2022 21:08 IST

For any programming language, it is very important how it is managing the memory allocation as it impacts the efficiency of machine. In this article we will discuss how python manages memory.

2022_07_MicrosoftTeams-image-23.jpg

The efficiency of any machine is greatly impacted by the characteristics of the codebase running within that machine.  Putting it simply, the process of efficiently managing the memory allocation by a programing language (like python) makes the programs written in that language more memory efficient that further leads to a better performance from the machine on which the software is running.

In this article, we are going to specifically look into the concepts of memory management in Python. Further ahead in the article we will be covering the following key concepts:

Now let’s look into each one of the above concepts in detail.

Garbage Collection in Python

In conventional programming languages like C++, the developers need to manually allocate and deallocate memory(also known as dynamic memory allocation), whereas in Python all of this process is built-in and automated using Garbage collectors. The garbage collector is responsible for freeing up space once the object stored in the memory is no longer of use. This is done so that the same space can be provided to a new object that needs it at that particular moment.

To better understand the concept take a look at the below code:

 
x = 5
y = x
if id(x) == id(y):
print("x and y are pointing to the same memory location")
else:
print("x and y does NOT point to the same memory location")
Copy code

Output

x and y are pointing to the same memory location

It is also important to note that Python also supports manual garbage collection. There are two types of manual garbage collection:

  • Time-based Garbage Collection: This is a type of manual garbage collection where the garbage collector executes itself after a fixed interval of time.
  • Event-Based Garbage Collection: In this type of garbage collection, we specify a particular event whose occurs triggers the garbage collector. For instance, when you exit an application, the space occupied by the same needs to be cleared up for further use by other objects.

Must Read: What is Python?

Must Check: Python Online Course & Certification

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Concept of Reference Counting:

Reference counting refers to the concept of counting the number of times an object is referenced by other objects in the system. This count gets altered each time a new reference to the object is added or removed.  These objects get deallocated from the memory once the reference count reaches zero.

For instance, consider a  variable(say x), with the value (say,100). Now the Python VM   will create an object for x with the value 100 in the private heap. 

x = 100 
2022_07_image-256.jpg

Now let’s create another variable(say, y), with the value same as the variable x.

x= 100
y= x
2022_07_image-257.jpg

Now doing this will make the variable y point to the original value stored in the private heap rather than creating another object of y in the private heap with the value 100.  Just to make sure that both x and y are referring to the same object run the below code:

 
x = 100
y = x
if id(x) == id(y):
print("x and y are pointing to the same object")
else:
print("x and y does NOT point to the same object")
Copy code

Output:

x and y are pointing to the same object

Now that we have established that both x and y are referring to the same object in the heap, let’s change the value of x and observer the behavior of the same code above.

 
x = 100
y = x
# increment the value of x by 10
x+=10
if id(x) == id(y):
print("x and y are pointing to the same object")
else:
print("x and y does NOT point to the same object")
Copy code

Output:

x and y does NOT point to the same object
2022_07_image-258.jpg

This behavior is because the variables x and y are not referencing the same object as x got changed. So now x creates a new reference object and y will still be referencing the original value of x ie, 100.

Must Read: What is Perfect Square?

Must Read: Rock Paper Scissors Game in Python

Memory Allocation in Python:

There are two parts of memory used by the Memory Manager to allocate space.

  • Stack memory
  • Heap Memory

Let’s take a look at each of them in detail.

Must Read: Type Casting in Python

Must Read: Literals in Python

The Stack Memory:

The stack memory allocation is used for storing static values. It is generally needed inside a particular method/function call. Generally, the size of memory to be allocated is already known to the compiler and the variables within the function get memory allocated on the stack on each function call. Here the allocation happens on contiguous blocks of memory. These temporary variables defined within the function get automatically deallocated once the function returns.

Example:

 
def testFunction():
#stack memory allocation
x = "Test"
y = 100
z = [1,2,3]
Copy code

The above variables x,y, and z only get access to memory once the testFunction() is called and they get deallocated once the function gets completely executed.

Must Read: How to Convert Celsius to Fahrenheit in Python

Must Read: Abstraction in Python

The Heap Memory:

This memory is the memory that is accessible to the developer. Any variable you define gets allocated some memory on the private heap of Python. These heap memories are not to be confused with the Dictionary data structure of python. It is a pile of memory space available to programmers to allocate and de-allocate. These are the variables that are defined outside the functions/methods.

Example:

 
x = 100
y = "TEST"
c = [1,2,3]
Copy code

The above variables x,y, and z only get access to the heap memory and they get allocated and deallocated as per the code instructions.

Must Read: Validating Passwords using Python Regex Match

Must Read: Python if-else statement

Conclusion

In this article, we explored the following concepts associated with memory management in Python:

  1. Garbage collection in Python
  2. Reference counting in Python
  3. Memory Allocation in Python.

Hope this article will help you in your Data Science journey.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio