Logo

Hardik Jaiswal

Kolkata, India hardikk.jaiswal@gmail.com GitHub

7 April 2025

How Python Handles Memory (And Why You Should Care)

by Hardik Jaiswal

Memory in Python: not just a boring comp-sci topic — it’s the silent MVP keeping your scripts from turning into memory-hogging monsters.

If you’ve ever wondered “Why is my Python code eating RAM like it’s at an all-you-can-eat buffet?” — congrats, you’re officially a dev now. Let’s unpack how Python handles memory under the hood. From reference counting to garbage collection to those mysterious things called arenas, you’re about to get a brain upgrade.


Reference Counting: Python’s First Line of Defense

Python tracks how many times an object is being used. This is called reference counting. When that count hits zero? Boom — memory is reclaimed.

a = [1, 2, 3]  # Count: 1
b = a          # Count: 2
c = a          # Count: 3

del b          # Count: 2
del c          # Count: 1
del a          # Count: 0 => Object gets deleted

Clean and simple — until it’s not.


But What About Circular References?

Imagine two objects pointing to each other in a “you hang up first” loop. Their reference counts never hit zero. That’s where Python’s Garbage Collector steps in like:

“I got this.”


Garbage Collection: Cleanup on Aisle Python

Python’s garbage collector is designed to deal with cycles — circular references that reference counting alone can’t clean up.

Here’s how it works:

import gc

gc.collect()  # Manual GC trigger

Wanna geek out? You can even tweak GC behavior:

gc.set_threshold(700, 10, 10)

Tuning garbage collection can be helpful in performance-critical apps or when you’re chasing down memory leaks like a bloodhound.


Behind the Scenes: Allocators, Pools & Arenas

Python’s got layers. Like, memory-management-croissant levels of layers.

Pymalloc: The Small-Object Specialist

For small objects (< 512 bytes), Python uses pymalloc to keep things quick and reduce fragmentation.

Pools & Arenas

This setup = faster memory allocation + less chaos.


Best Practices for Memory Mastery

Want to write Python like a memory-efficient wizard? Here’s the cheat sheet:

from memory_profiler import profile

@profile
def big_memory_func():
    return [i for i in range(1000000)]


Final Thoughts

Python does a lot for you behind the scenes when it comes to memory. But understanding how it all works lets you:

So go ahead, open that gc module, mess with sys.getrefcount(), and see what’s going on under the hood. Because memory management isn’t just some behind-the-scenes stuff — it’s Python magic in motion.

💬 “This was fire! What topic should I break down next? Ping me on GitHub or drop a comment.”

tags: Python - CMD - SQLite3