🎩 Master Quiz: Memory Management
20 Questions based on the “Warehousing & Cleaning Crew” analogy.
Green = Correct, Red = Incorrect.
In the Warehouse analogy, what corresponds to the “Heap Memory”?
💡 Explanation
The Shelves (Data storage).
The Heap is the physical area where objects (products) are stored. The Stack is just the catalog of references (labels).
What happens in memory when you run x = 10?
💡 Explanation
Two-step process: Heap object, Stack reference.
Python creates the object 10 first (if not cached) in the Heap, then creates the label x in the Stack pointing to it.
What happens if you run x = y?
💡 Explanation
It copies the pointer.
Both variables end up pointing to the exact same object in the Heap. No new object is created.
What is Python’s Primary mechanism for Garbage Collection?
💡 Explanation
Reference Counting.
This is real-time. As soon as the count hits zero (no labels left), the object is destroyed immediately.
Why do we need a “Secondary” GC mechanism (Generational GC)?
💡 Explanation
To handle Cyclic References.
If Object A points to B, and B points to A, their ref count never hits zero. The Generational GC scans for these isolated loops.
How many “Generations” does the Cyclic Garbage Collector use?
💡 Explanation
3 Generations (0, 1, 2).
New objects start in Gen 0. If they survive a cleanup, they are promoted to Gen 1, and eventually Gen 2.
What is the range for “Small Integer Caching” (Interning)?
💡 Explanation
-5 to 256.
These integers are pre-allocated singletons. Every time you use them, you reference the existing object rather than creating a new one.
Which tool allows you to see the number of references to an object?
💡 Explanation
sys.getrefcount(obj).
This debugging tool shows how many variables are currently pointing to a specific object.
What does the id(obj) function return?
💡 Explanation
The unique memory address.
It’s like the physical shelf number in the warehouse. You use it to check if two variables point to the exact same location.
What is the “Stop-the-World” problem?
💡 Explanation
A micro-pause.
When the GC runs on Generation 2 (old objects), it may briefly pause the program. This is critical to know for high-frequency trading apps.
What is the difference between == and is?
💡 Explanation
is checks memory IDs.
a is b returns True only if they point to the same object. a == b checks if the data inside is equivalent.
How can you force a Garbage Collection cycle manually?
💡 Explanation
gc.collect().
While rarely needed, you can use this in heavy scripts to force the “Cleaning Crew” to run immediately.
What does the command del x do?
💡 Explanation
Removes the name and decrements count.
You never delete objects manually. You just remove the label. If the count hits zero, the GC handles the deletion.
Why does a Python integer take 28 bytes vs 4 bytes in C?
💡 Explanation
The Object Header.
Every Python object carries metadata (type, reference count) which adds significant overhead compared to raw C data types.
What common C/C++ error does Python prevent?
💡 Explanation
Segmentation Faults.
Because you don’t manually manage memory (malloc/free), you avoid accessing invalid memory addresses, making Python safer.
Where do objects like lists and functions live?
💡 Explanation
On the private Heap.
All actual data objects live in the Heap. The Stack only contains the references (names) pointing to them.
If a script eats RAM over 2 days, what is the likely cause?
💡 Explanation
A Reference Cycle.
Complex structures referring to each other can fool the reference counter. If the Generational GC doesn’t run or clean them, RAM usage grows.
What lives on the “Stack”?
💡 Explanation
References (Variable Names).
The Stack is fast and structured, used for managing function calls and variable names (pointers), while the heavy data sits in the Heap.
Does Python require malloc or free?
💡 Explanation
No, it is Automatic.
The CPython Memory Manager handles all allocation and deallocation behind the scenes.
Why is understanding id() important for optimization?
💡 Explanation
To check for needless copying.
If two variables have different IDs when they should be sharing data, you might be wasting RAM by duplicating objects.