Card 09/ 09
Recap: Stack, Heap and Garbage Collection
Two regions, one rule for what survives, two errors and one kind of leak, with where each was.
Memory in Java comes down to the five ideas below, each listed with the card that covers it.
| The idea | Where |
|---|---|
| Variables live in stack frames; every object lives on the heap | The Stack and the Heap in Java |
| An object survives while a path from a root reaches it, and no longer | Garbage Collection and Reachability |
| A stack overflow is recursion; an out-of-memory is reachable objects | StackOverflowError vs OutOfMemoryError |
System.gc() requests a collection and cannot free anything reachable | System.gc() Is Only a Suggestion |
| A leak is code holding references it has no use for | Memory Leaks in Java: References You No Longer Need |
Reachability, not usefulness, is the whole rule. The collector has no idea what you are finished with, so anything still on a path from a static field, a live frame or a running thread stays exactly where it is.
← → move through sections and cards