Python › Memory Management & Garbage Collection

What does it mean that Python has 'automatic' memory management, and what's the trade-off compared to manual memory management in languages like C?

Beginner

Answer

Python programmers don't need to explicitly allocate or free memory -- the interpreter handles allocation on object creation and deallocation via reference counting and garbage collection. The trade-off is less direct control and some performance/predictability overhead compared to manual memory management, but it eliminates entire classes of bugs like use-after-free, double-free, and most memory leaks.
# No malloc/free needed
data = [i**2 for i in range(1000000)]
# automatically freed when 'data' goes out of scope or is reassigned
Real-world example A team migrating a performance-critical component from Python to C++ has to now manually manage memory lifetimes that Python previously handled transparently, introducing new categories of bugs.

Common follow-ups: What memory bugs does automatic management prevent?;When would manual memory control still matter in Python (e.g., via ctypes)?