Python / Others / Implementation / Memory Management

Python Memory Management

By Marcelo Fernandes Ago 08, 2017

You may Jump to the Shortcut

Memory Management:

One benefit of using python over other languages is that it automatically manages memory, the user does not have to preallocate or deallocate memory by hand, as one has to do when programming in languages such as C or C++.

Another thing to keep in mind is that, in python, everything is an object

x = 10
# <class 'int'>

Every time that a variable is declared in python, it creates a reference to an object that is inside the heap memory. If we have multiple variables, that hold the same object, two references are gonna be created, pointing to the same object.

x = 10
y = 10
z = x

if id(x) == id(y) == id(z):
    print('x, y and z refer to the same object')
# x, y and z refer to the same object

x = 11
if id(x) != id(z):
    print('x and z refer to DIFFERENT objects')
# x and z refer to DIFFERENT objects

This represents the memory optimization concept behind python. If a variable is declared to an object that already exists, its not going to create a new one.

Memory Representation

Every time that a python instance is ran, the python interpreter is going to reserve some space in the memory for the execution. This space is going to be divided by two different memory spaces: Stack Memory and Heap Memory

Heap memory

Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.

Stack memory

The stack memory is responsible for creating stacks of references to objects. Every variable has its reference pointing to an object on the heap memory, therefore, the place where those references live is called Stack Memory. When a function is called in python, it creates its own stack in order to control its own references, and once the return is reached, the stack itself will be deleted.

Garbage Collector

The Garbage Collector is the responsible for deleting those objects that have no references pointing to them. Different python implementations deal with different garbage collector algorithm. In the cPython case, it uses the Refereces Counting Algorithm. In this case, the interpreter keeps track of the objects in a table, and as soon as an object has the number of references set to 0, it is deleted from the memory.

class Car:
    def __init__(self, w):
        self.wheels = w

c1 = Car(6)
c2 = c1

c3 = Car(6)

Garbage Collector Table:
Object Number of References
Car object 1 2
Car object 2 1

del c1
del c3

Garbage Collector Table:
Object Number of References
Car object 1 1
Car object 2 0 (will be deleted from memory)

As soon as the garbage collector realizes that there is an element with 0 references, it will be wiped out from the memory.


  • The methods and variables are created on the stack memory
  • The objects and instances variables are created on the heap memory
  • A new stack frame is created on invocation of a function/method
  • Stack Frames are destroyed as soon as the function/method returns.
  • Garbage collector is a mechanism to clean up the dead objects.



link 1
link 2
link 2