Stack versus heap, reference counting, mark and sweep, generational GC, manual management, leaks and fragmentation.
35 cards · basic cards · AI-written, checked twice. Edit anything.
- What is the stack in memory management?
Show answer
A region of memory that stores local variables and function call information, following LIFO order.
- What does LIFO mean in the context of the stack?
Show answer
Last In, First Out - the most recently added item is removed first.
- When are stack variables automatically freed?
Show answer
When the function they are declared in returns or exits its scope.
- What causes a stack overflow?
Show answer
Allocating more memory on the stack than its fixed size allows, often due to excessive recursion or large local arrays.
- Where are local variables typically stored?
Show answer
On the stack, where they are automatically deallocated when their function exits.
- What is the heap in memory management?
Show answer
A large, dynamically-allocated region of memory used for objects whose size or lifetime cannot be determined at compile time.
- Why is heap allocation slower than stack allocation?
Show answer
The heap requires dynamic memory management via algorithms like searching for free blocks, whereas the stack uses simple pointer arithmetic.
- What is external fragmentation on the heap?
Show answer
Gaps of unused memory scattered throughout the heap between allocated blocks, making it hard to find contiguous free space.
- Can objects on the heap be automatically freed without garbage collection?
Show answer
No, they persist until explicitly deallocated or collected.
- What is internal fragmentation?
Show answer
Wasted memory within an allocated block due to the block being larger than the object it holds.
- How does reference counting work?
Show answer
Each object maintains a count of how many pointers reference it; when the count reaches zero, the object is freed.
- When is an object freed in reference counting?
Show answer
Immediately when its reference count drops to zero.
- What is the circular reference problem in reference counting?
Show answer
Two or more objects that reference each other form a cycle where counts never reach zero, causing a memory leak even with reference counting.
- What is the overhead of reference counting?
Show answer
Every pointer assignment requires incrementing or decrementing a count, consuming CPU time and potentially adding cache misses.
- Must reference count operations be atomic in concurrent systems?
Show answer
Yes, to prevent race conditions from multiple threads accessing the same count simultaneously.