Skip to main content

Command Palette

Search for a command to run...

Inside Python's Brain: How Memory Works with Mutable and Immutable Objects

Published
3 min read

Have you thought ever How Python manages it's memory?How it stores a variable internally?How it some value changed and some we can't modify it how it is determined by Python?How it internally treats different data structures?

There are lots of question regarding Python memory management.Let’s hack brain of Python and get idea about how it handles its memory so efficiently.

Internal working of memory

In Python all data structures treated as Object.Python internally uses reference pointer and garbage collector for memory management.Python stores all object in heap memory.Every time you create a new object (like an int, list, str, function, class instance, etc.), Python allocates memory for it on the heap.and its reference to that object in heap.Python allocates and deallocates memory according to its need.If one loose reference of object in Python it stores in garbage collector and automatically removed.

x=10
y=10

print(id(x))#140145097611624
print(id(y))#140145097611624

In above case x and y referencing towards same memory location as it's value is same.

x=10
y=10
x=x+1
print(id(x))#136634722310536
print(id(y))#136634722310504

In next example when I change x value then reference of memory for x is changed from y.

So from this,I am going through mutable and immutable state of python.
In Python some objects are immutable and some are mutable.Immutable means after creation of value its value cant be changed.And mutable is obviously the opposite one.It’s value can be changed after creation.
In python list, dict, set, bytearray, custom classes are mutable and int, float, bool, str, tuple, frozenset, bytes immutable.

Mutable Object Example-

a = [1,2,3]
b=a
print(id(a))  # 133336479893952

b.append(4)
print(a)       # Output:[1, 2, 3, 4]
print(b)       # Output: [1,2,3,4]
print(id(b))   # 133336479893952

So list is changing its value in Python and memory address remain same.

Immutable Object Example-

x = 10
y = x
y = x + 1

print(x)  # 11
print(y)  # 10

print(id(x))  # Different ID 137671584926088
print(id(y))  # Original ID 137671584926056

In case of Immutable object memory address is changing after increasing value to a.So it’s showcase the differences between both.Mutable Object share references..When changing one value others value also get changed.And Immutable object always change references with changing of its value.

Difference between Mutable and Immutable Object

So for mutable object when changing it’s value or appending to it memory location intacted. But for immutable object changing it’s value reference towards variable changed.For Mutable same object get changed and in case of immutable object new object is created

This is how Python manages it’s datatype smartly.So we can use datatypes according to their features.So this is how python manages it's memory dynamically with respect to data structure.So I hope,Python's memory management get stuck into your memory.