Memory Management in Python
Memory management refers to process of allocating and deallocating memory to a program while it runs. Python handles memory management automatically using mechanisms like reference counting and garbage collection, which means programmers do not have to manually manage memory.
Let's explore how Python automatically manages memory using garbage collection and reference counting.
Garbage Collection
It is a process in which Python automatically frees memory occupied by objects that are no longer in use.
- If an object has no references pointing to it (i.e., nothing is using it), garbage collector removes it from memory.
- This ensures that unused memory can be reused for new objects.
For more information, refer to Garbage Collection in Python
Reference Counting
It is one of the primary memory management techniques used in Python, where:
- Every object keeps a reference counter, which tells how many variables (or references) are currently pointing to that object.
- When a new reference to the object is created, counter increases.
- When a reference is deleted or goes out of scope, counter decreases.
- If the counter reaches zero, it means no variable is using the object anymore, so Python automatically deallocates (frees) that memory.
Example:
a = [1, 2, 3]
b = a
print(id(a), id(b)) # Same ID â both point to same list
b.append(4)
print(a)
Output
140645192555456 140645192555456 [1, 2, 3, 4]
Explanation:
- a and b both refer to same list in memory.
- Changing b also changes a, because both share same reference.
Now that we know how references work, letâs see how Python organizes memory.
Memory Allocation in Python
It is the process of reserving space in a computerâs memory so that a program can store its data and variables while it runs. In Python, this process is handled automatically by interpreter, but the way objects are stored and reused can make a big difference in performance.
Let's see an example to understand it better.
Example: Memory Optimization with Small Integers
Python applies an internal optimization called object interning for small immutable objects (like integers from -5 to 256 and some strings). Instead of creating a new object every time, Python reuses same object to save memory.
Suppose:
x = 10
y = 10
Here, Python does not create two separate objects for 10. Instead, both x and y point to the same memory location. Let's verify if it's true:
x = 10
y = x
if id(x) == id(y):
print("x and y refer to same object")
Output
x and y refer to same object

Now, if we change x to a different integer:
x = 10
y = x
x += 1
if id(x) != id(y):
print("x and y do not refer to the same object")
Output
x and y do not refer to the same object
When x is changed, Python creates a new object (11) for it. The old link with 10 breaks, but y still refers to 10.

In Python, memory is divided mainly into two parts:
- Stack Memory
- Heap Memory
Both play different roles in how variables and objects are stored and accessed.
Stack Memory
Stack memory is where method/function calls and reference variables are stored.
- Whenever a function is called, Python adds it to the call stack.
- Inside this function, all variables declared (like numbers, strings or temporary references) are stored in stack memory.
- Once the function finishes executing, stack memory used by it is automatically freed.
In simple terms: Stack memory is temporary and is only alive until the function or method call is running.
How it Works:
- Allocation happens in a contiguous (continuous) block of memory.
- Pythonâs compiler handles this automatically, so developers donât need to manage it.
- It is fast, but it is limited in size and scope (only works within a function call).
Example:
def func():
# These variables are created in stack memory
a = 20
b = []
c = ""
Here a, b and c are stored in stack memory when function func() is called. As soon as function ends, this memory is released automatically.
Heap Memory
Heap memory is where actual objects and values are stored.
- When a variable is created, Python allocates its object/value in heap memory.
- Stack memory stores only the reference (pointer) to this object.
- Objects in heap memory can be shared among multiple functions or exist even after a function has finished executing.
In simple terms: Heap memory is like a storage area where all values/objects live and stack memory just keeps directions (references) to them.
How it Works:
- Heap memory allocation happens at runtime.
- Unlike stack, it does not follow a strict order itâs more flexible.
- This is where large data structures (lists, dictionaries, objects) are stored.
- Garbage collection is responsible for cleaning up unused objects from heap memory.
Example:
# This list of 10 integers is allocated in heap memory
a = [0] * 10
Explanation: Here, list [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] is stored in heap memory. The variable a in stack memory just holds a reference pointing to this list in the heap.