How memory is managed in Python? | Complete Guide

Sep 4th, 2023

How memory is managed in Python? | Complete Guide


Memory allocation is a very important aspect of any programming language. As software developers, we must know about it to write efficient code (i.e. memory-efficient code). Memory management determines how a language handles the allocation or deallocation of memory resources for its variables, objects, and data structures.

Python is a very popular and flexible programming language and has a unique way of managing memory that makes it easy to work but it also comes with some trade-offs. In this article, we’ll discover how memory is managed in Python.

Dynamic Typing and Object-Oriented Nature

Python is a dynamically typed language, which that means while creating a variable, you don’t need to specify the data type of that variable explicitly. Instead, Python determines the data type during the runtime.

Everything in Python is an object, from integers and strings to complex data structures like lists and dictionaries. Objects in Python are stored in memory, and the memory management system plays a crucial role in the allocation and deallocation of memory for these.

Garbage Collection

Garbage collection is the process that periodically identifies and collects objects that are no longer reachable and deallocates their memory.

Reference Counting

Imagine you have a library with a collection of books. Assume that each book represents an object in Python, and the library’s catalog represents the reference count.

Let’s understand with a short story

Let’s assume Jack has picked a book “X” from the shelf. This creates a reference to the Librarian (Python Object). This makes Jack responsible for it. This is analogous to creating a reference in Python by assigning the object to a variable.

Now Jack returns the book to the library, it’s like releasing a reference to a Python object. The book is no longer his responsibility. Similarly, in Python when a variable goes out of scope or is reassigned to another object, it’s like returning the reference to the object.

Now Jack’s reference with the book “X” has been removed.

Just like books memory is available unless it is assigned to an object (person).

Let’s understand it programmatically

The reference counting mechanism keeps track of how many times an object is referenced by other objects in the system. When the reference count becomes zero, then that object is deallocated.

For example, let us declare two variables assigning the same value. Instead of creating another object of the same value in the heap, it makes the other variable point to the original value in the heap memory. This helps save a large amount of space in the memory.

For example,

x = 24
y = x

Reference Counting Illustration

Here, when the code is executed, an integer 24 is created in memory and is pointed towards the reference variable x.

Now, we are assigning the variable y to x. Since they both have the same values, variable y is pointed towards the object of variable x as they both have the same values instead of creating a new object.

We can verify if that’s true.

print(id(x) == id(y))
# Returns true if they refer to the same object




In the world of programming, efficient memory management is essential to create robust and high-performing applications. Python, with its dynamic typing and object-oriented nature, offers a unique approach to memory management. By understanding the key principles of memory allocation, reference counting, and garbage collection, developers can write more memory-efficient code and build applications that are not only powerful but also resource-conscious. meme

Enjoy the meme…

I hope you have learnt something new from this article. Thanks for reading and have a nice day!