Memory Management in Python

If you want to know about Memory Management in Python then you are at the right place, please go through this article, you will be familiar with Memory Management and Garbage Collection in the Python Programming Language.

Keynotes to Remember during writing your code

  • If there is no more reference to an object then it is safer to remove from memory.
  • Avoid using a complex object or a large object in Global namespace.


  • Reference
  • Object

Memory Management and Garbage Collection in Python

Why should we care for the memory?

  • If you want to write a more efficient code in Python then you must need Memory Management skills.
  • Python deals with two Memory segment:- Stack Memory and Heap Memory
  • In Python whenever we create any variable and assigned a constant value to it, the variable name is stored in stack memory and constant value will be stored in Heap memory.

Memory managed in the following order as written below:-

  • Python Object Allocator (Integer, List, String…etc.)
  • Under Python Object Allocator there are 4 segments:-
Private Heap --> Python Raw Memory allocator --> 
General-purpose allocator (malloc) --> OS MemoryManager
  • Python manages its Object by keeping track of its reference number. This means that Python counts the number of references created during the program execution.
NOTES:- You can get the number of references for any objects using getrefcount().

For example:-

import sys
Print(sys.getrefcount (a)) # o/p = 2
Print(sys.getrefcount(b)) # o/p = 2
del(a) #delete reference to a
del(b) #delete reference to b
del command 
        Delete the reference to the object.
        Reduce the number of reference (getrefcount) by 1.

Garbage Collection

  • We are too lazy to remove the unwanted memory. Since Nowadays we had enough memory to use therefore this unwanted occupied memory didn’t affect the system and hence we avoid releasing memory manually.
  • But somehow it affects the performance of code execution and leads to memory fragmentation if it continued in the same way, so Python Developer develops the concept of Garbage collection.
  • Garbage collection: It is defined as a procedure for a program by which the program automatically release memory when the object that occupied space is no longer in use further.
  • There are two methods for Garbage collection
    1. Reference Counting
    2. Tracing

1. Reference Counting:-

  • It includes the addition and removal of references and keeps the record of the number of references in reference counter.
  • When an object is deleted, its reference counter decreases by 1 every time.
  • If the reference counter reached 0, it removes the object.
  • Characteristics of Reference Counter:
    • a. Easy to implement (no need to track references manually).
      b. When the reference count reaches 0, objects are deleted immediately.
      c. Space and Execution overhead, since for every assignment reference count change.
      d. It is not generally thread-safe.
      e. The reference counter doesn’t detect cyclic references.

2. Tracing:-

  • In programming, tracing refer to the automatic memory cleaning process. Python uses the Mark and Sweep algorithm for Tracing the objects.
  • In other words, it uses the Generational approach. This is to say that the Generational approach based on the theory that  Most objects die young.
  • Further Generational approach had three steps:-
    Generation 0 –>Generation 1 –> Generation 2
  •   When the number of objects in a generation reached a threshold, firstly Python runs Garbage collection on the generation, and also on any generation younger than it. Secondly, after garbage collection objects promoted to the next generation.

GIL – Global Interpreter Lock

  • According to GIL, only one thread can run in the interpreter.
  • Reference counting is fast and easy to implement.
  • No matter how many threads exist, only one will execute at a time.

Find memory address in Python

Leave a Reply

Your email address will not be published. Required fields are marked *