Skip to content
Ameen Abozaid
BEG-038

Java memory basics

  • 5 Lessons
  • 50 min of reading
  • 1.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Stack and heap conceptual model
  • object references
  • garbage collection introduction

Learn first

50 min of reading · 1.5 h of practice (estimate)

Learning outcomes

  1. Draw the stack and heap for a short program, showing frames, local variables, and objects.
  2. Explain why two variables can refer to the same object and what that implies for mutation.
  3. Explain at a high level when an object becomes eligible for garbage collection.

Lessons

  1. 01Stack and heap: a picture10 min read
  2. 02References and aliasing10 min read
  3. 03Garbage collection, gently10 min read
  4. 04OutOfMemoryError and StackOverflowError10 min read
  5. 05Demo: drawing stack and heap, then checking in the debugger10 min read

Lab and exercises

Lab

BEG-038-LAB1 · Draw it

Outcomes: 1–3 · Time: ~1 h Draw memory diagrams (stack frames, local variables, heap objects) for four short programs: primitive copy, object aliasing, a method that mutates a parameter, and an object that becomes unreachable. Confirm each with the debugger. Acceptance: diagrams match observed values.

Exercises (assessment: diagram quiz)

IDOutcomeTask
BEG-038-EX011Draw the stack and heap for int x = 5; int y = x; y = 6; StringBuilder sb = new StringBuilder("a"); and state x.
BEG-038-EX022Given StringBuilder a = new StringBuilder("hi"); StringBuilder b = a; b.append("!"); predict a, and explain.
BEG-038-EX032A method void add(List<String> l) { l.add("z"); } is called with a list. Does the caller's list change? Explain.
BEG-038-EX043Mark the moment the object becomes eligible for GC: Person p = new Person(); p = new Person();.
BEG-038-EX053Explain in two sentences why System.gc() does not guarantee anything.
Show solutions
  • EX01 x is 5; y is 6 (copied by value). sb in the frame points to a heap StringBuilder.
  • EX02 a is "hi!": b is another reference to the same object.
  • EX03 Yes. The method receives a copy of the reference, pointing to the same list.
  • EX04 After the second assignment, the first Person has no references and is eligible.
  • EX05 It only suggests a collection; the JVM decides when and whether it runs, and garbage is collected automatically when unreachable.
  • The four-program diagram work in the lab and the diagram quiz (the module quiz) cover outcome 1 as well.

Quiz

Outcomes: O1 stack and heap · O2 references and aliasing · O3 garbage collection.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Local variables of a running method live in:O1
  2. 2Objects created with new live:O1
  3. 3When a method returns, its stack frame:O1
  4. 4int a = 5; int b = a; b = 9; then a is:O2
  5. 5List<String> a = new ArrayList<>(); List<String> b = a; b.add("x"); then a.size() is:O2
  6. 6b = a for object variables copies:O2
  7. 7== on two object variables compares:O2
  8. 8An object is eligible for GC when:O3
  9. 9System.gc():O3
  10. 10Infinite recursion causes:O1

Practical part

Five exercises BEG-038-EX01 to EX05 plus the lab diagrams. Pass: at least four exercises correct and all four lab diagrams matching the debugger.

Common mistakes

Thinking objects are copied on assignment; believing System.gc() frees memory on demand.

Assessment

Diagram quiz.

Career relevance

JVM memory management (MID-002) and leak diagnosis (MID-008).

Type to search levels, phases, modules and lessons.