Heap, Stack, GC Overview
Local variables and call chains are located on the thread stack. Objects are located on the heap and are released by the garbage collector when they become unreachable.
StackOverflowError: too deep calls. OutOfMemoryError: heap full.
Where used
Allocation pressure, escape analysis, GC pauses as a latency cause. Stack frames explain recursion limits; the heap explains object graphs and leaks.
Depth
Each thread manages call frames with local variables and return information on its stack. Objects are typically allocated in a shared memory area; local references can point to these objects. Therefore, a local variable does not automatically make the referenced object thread-local.
The garbage collector identifies objects that are no longer reachable from roots such as active stacks and static fields. It decides on memory release, not on the timely closing of external resources. Files and sockets therefore require explicit lifetime management.
Difficulty levels
- Distinguish between local reference and referenced object.
- Track reachability from GC roots.
- Separate memory cleanup and resource release.
Pitfalls
An object is not collected simply because a single reference goes out of scope. As long as another reachable path exists, it remains alive. System.gc is also not a reliable guarantee of release.
Tasks
Card Info
- Topic: Algorithms and Data Structures
- Difficulty: Beginner
- Completed: 0 users