Layers of Logic

Phase XIII

Memory and the Garbage Collector

Three phases have argued about the heap. This is where you find out what it costs.

2 sections41 min of reading8 exercises

You have been told that local variables live on the stack and objects live on the heap since Phase IV, and you have taken it on trust. This phase opens both, adds the two areas nobody mentions, and then explains the part that has no equivalent in C: nobody frees anything, and a program decides on its own which of your objects are still needed. Ten million Integer objects has been an argument three times now. Here it is 192 MB against 40 MB.

When you finish this phase you can

  • Name the memory areas a running JVM has and say what each one holds
  • Explain why a local variable dies at the closing brace and an object does not
  • Say what makes an object garbage, in the exact sense the collector uses
  • Read the generational idea and say what observation it is built on
  • Explain what a stop the world pause is and why collectors are judged on it
  • Say what a memory leak looks like in a language that has no free
  1. 13.1Where Everything Actually LivesYou have been told since Phase IV that locals live on the stack and objects live on the heap. Both halves are true and neither is the whole picture, and the two areas nobody mentions are where your classes and your string literals are.The memory areasCore20 min4 exercises
  2. 13.2What Makes an Object GarbageNobody frees anything, and a program running alongside yours decides which of your objects are still needed. The rule it uses is not the obvious one, and knowing the real rule is what lets you find a leak in a language that has no free.Garbage collectionCore21 min4 exercises