Understanding Python Memory Management
Chelsea Dole
Data Engineer
What is “memory”, and what manages it in Python?
“test”
0x1045f8b70 |
0x1045adaf0 |
0x1022c6934 |
... |
TYPE: int, VALUE: 1000, REF COUNT: 1
“test”
0x1045f8b70 |
0x1045adaf0 |
0x1022c6934 |
... |
TYPE: int, VALUE: 1000, REF COUNT: 1
“another_test”
TYPE: int, VALUE: 1000, REF COUNT: 2
So, what was that “ref count” thing?
Increasing Ref Count:
Decreasing Ref Count:
1000
Garbage Collection!
🎉
🎉
🚛
🚛
Source code is faster and uses less memory. Bug prone for developers.
Source code is more complex, and uses more memory. Easier for developers.
However, ref counting isn’t reliable enough to be Python’s exclusive method of garbage collection
Solution: generational “Mark and Sweep” algorithm
How a language chooses to manage memory is a major functional and philosophical choice
Python chooses to maintain a complex and relatively slow memory management system internally, because it enables Python to be simple, beautiful, and readable for developers.
Thank you!
Chelsea Dole