Table of contents
1.
Introduction
2.
What is Memory management?
3.
What is Python Memory allocation?
3.1.
Static Memory Allocation
3.2.
Dynamic Memory Allocation
4.
What is Memory deallocation?
5.
What is Reference Counting in Python?
5.1.
Code
5.2.
Python
5.3.
Output
5.4.
Explanation
6.
Python Objects in Memory
7.
Role of Garbage Collection in Python
8.
Importance of Manual Garbage Collection
9.
Frequently Asked Questions
9.1.
Is there memory management in Python?
9.2.
How do you use memory efficiently in Python?
9.3.
How do I check memory management in Python?
9.4.
Does Python use stack or heap?
9.5.
How is Python’s garbage collector implemented?
9.6.
Can we manually deallocate memory in Python?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

Understanding Python Memory Management

Author Akshat Aggarwal
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python has gained popularity as a high-level programming language for web development, scientific computing, and data analysis applications. Understanding memory management is vital for writing memory-efficient code. In Python, memory management is done automatically, meaning we don't have to allocate or deallocate memory explicitly. This is unlike other programming languages like C/C++, where we have to manually allocate and deallocate memory using different functions like calloc(), malloc(), and free().

Python Memory Management

This article will discuss the basics of Python Memory Management.

What is Memory management?

Memory management is the process of allocation and deallocation of memory resources in a computer system. Python's internal memory manager manages memory using reference counting and garbage collection.

What is Python Memory allocation?

Memory allocation refers to reserving memory for storing data in a computer system. There are two types of memory allocation.

There are two types of memory allocation.

  1. Static Memory Allocation
     
  2. Dynamic Memory Allocation
types of memory allocation

Static Memory Allocation

In Static Memory Allocation, memory is allocated for variables, objects, data structures, etc., at compile time and fixed throughout the program's running. C and C++ are some programming languages that use Static Memory Allocation.

After static allocation of memory, changing it is not possible. Static memory is also called Stack memory because it uses a stack data structure to allocate memory. It allocates contiguous blocks of memory.

Stack Memory

Dynamic Memory Allocation

In Dynamic Memory Allocation, memory is allocated dynamically at the runtime of a program. This means more memory is allocated if it is needed. Python and Java are some programming languages that use Dynamic Memory Allocation.

We can change and reuse dynamically allocated memory. Dynamic memory is also called Heap memory because it uses a heap data structure to allocate and deallocate memory. Heap memory is a large pool of memory available for dynamic allocation.

For example, an integer takes a different amount of memory than a string. Thus the size of the memory block allocated for a variable depends on its type and is dynamically allocated.

Heap Memory

Also see, Python Filter Function

What is Memory deallocation?

Memory deallocation (Memory release) is freeing previously allocated memory. In Python, memory deallocation is automatic. The garbage collector periodically scans the memory to identify and release objects no longer referenced by the program, allowing the program to use that memory for other purposes.

What is Reference Counting in Python?

In Python, Reference counting is a technique that automatically manages memory allocation and deallocation. In this technique, the memory manager keeps track of the number of references to each object in memory. An object's reference count is incremented whenever a new reference to that object is created and decremented whenever a reference to that object is removed or goes out of scope.

When an object's reference count reaches zero, there are no more references to that object in the program, and the object can be safely deallocated. This is done implicitly.

For example,

Code

  • Python

Python

import sys

def check(a, b):
   print("a = ", a)
   print("b = ", b)
   if id(a) == id(b):
       print("a and b refer to the same object")
   else:
       print("a and b dont refer to the same object")

a = [1, 2, 3]
print("Count of references = ", sys.getrefcount(a))
print()

b = a
check(a, b)
print("Count of references = ", sys.getrefcount(a))
print()

b = 5
check(a, b)
print("Count of references = ", sys.getrefcount(a))
print()
You can also try this code with Online Python Compiler
Run Code

Output

Output

Explanation

In this program, we use Python's id() function to check if two objects refer to the same object. Initially, there was only one reference to the list, i.e., from a.

NOTE: Please note that the sys.getrefcount() function shows an extra reference each time because passing the object as an argument to the function adds another reference to the object.

list has one reference

After the initialisation of b, the references to the list increase.

list has two references

When we change the value of b from a list to a number, it stops referring to the list object and starts referring to another object in memory.

a and b both point to different objects now

Also see, How to Check Python Version in CMD

Python Objects in Memory

Python objects are stored in memory with unique identifiers (object identity) and values and the type of an object can change dynamically. Objects are created when they are assigned to variables, and a reference counting mechanism manages their memory. Garbage collection reclaims memory when objects are no longer referenced.

In Python, everything is considered an object. This means that all data, including integers, strings, functions, classes, and even modules, are treated as objects with associated attributes and methods. 

Role of Garbage Collection in Python

Garbage collection is a feature of Python which helps manage memory. It reclaims memory that is no longer referenced and thus no longer needed. If the refcount() of an object is 0, it is no longer needed.

The garbage collector runs periodically and searches for useless objects, which affects the program's performance. It deallocates the memory of such objects when it finds them. Garbage collection in Python prevents memory leaks and ensures efficient use of memory. 

Also see, Swapcase in Python, and  Convert String to List Python

Importance of Manual Garbage Collection

Python's automatic garbage collector is effective, but it doesn't work for circular references, where objects reference each other in a way that prevents their reference count from dropping to zero. Manual garbage collection using the GC module can help in such cases.

Also, in cases where an object holds non-memory resources such as file handles, sockets, or database connections, manual garbage collection can be used to explicitly release these resources when they are no longer needed, even if the memory management is automatic.

Frequently Asked Questions

Is there memory management in Python?

Yes, Python has automatic memory management. It uses reference counting and a cyclic garbage collector to allocate and deallocate memory for objects efficiently.

How do you use memory efficiently in Python?

To use memory efficiently in Python, avoid unnecessary object creation, release resources explicitly, use generators, and optimize data structures for your specific needs.

How do I check memory management in Python?

To check memory usage in Python, you can use tools like sys.getsizeof(), memory profiling libraries like pympler, or system-level tools like psutil.

Does Python use stack or heap?

Python uses both stack and heap memory. Stack for function call frames and local variables, and heap for dynamically allocated objects.

How is Python’s garbage collector implemented?

Garbage collection is implemented in Python in two ways: reference counting and generational.

Can we manually deallocate memory in Python?

No, we cannot manually deallocate memory in Python. Python memory management is automatic.

Conclusion

This article discussed Python memory management and techniques like reference counting and garbage collection. Understanding these is important for writing memory-efficient programs.

To learn more about Python and memory management, we recommend reading the following articles:

Remember to refer to our guided paths for complete interview preparation guidance. This amazing Data Structures and Algorithms course will enhance your DSA skills. You can also try the Competitive Programming course and upskill your CP skills.

Check out the free mock test series to test your knowledge. If preparing for tech interviews, check out the problemsinterview experiences, and interview bundles for placement preparations.

Live masterclass