Unit 1 · Lesson 715 minAcademic review pending

The bug you can no longer write

CS105ES made you free every malloc yourself, and forgetting was a real, silent bug. Java removes that responsibility entirely. This lesson is about exactly what replaces it, and what new limits come with the trade.

Choose explanation

After this lesson

You should be able to

  • Explain what garbage collection does and when an object becomes eligible for it.
  • State why Java has no equivalent of free or a dangling pointer.
  • Recognise that garbage collection has a real cost, not just a benefit.
01

The C bug this removes

In CS105ES, every malloc came with a debt: somewhere later, you had to call free on that exact pointer, or the memory stayed reserved forever — a memory leak. Free it twice, or use a pointer after freeing it, and you got a dangling pointer bug, one of the hardest categories of bug to debug in C, because the program often keeps running for a while before it visibly breaks.

Java removes this responsibility from you entirely. There is no free keyword in Java at all — you simply cannot write a double-free or dangling-pointer bug, because there is no operation that lets you manually release memory. An object stops taking up space only when nothing in the program can reach it any more.

02

Eligible, not gone: how the JVM decides

An object becomes eligible for garbage collection the moment nothing in your program can reach it any more — no variable, no field, no array slot holds a reference to it. Reassigning a reference to something else, or letting a local variable go out of scope at the end of a method, are the two most common ways that happens.

Eligible does not mean instantly reclaimed. The JVM runs a background process, the garbage collector, which decides on its own schedule when to actually reclaim eligible objects — you cannot force an exact moment, only make an object eligible. This is a deliberate trade: you give up precise control over timing, and in exchange, an entire category of bugs becomes structurally impossible to write.

Eligible the moment nothing points at it
Student s = new Student(101, "Kavya");  // object created, s references it
s = null;                               // no variable references it any more
// the original Student object is now eligible for garbage collection
// exactly when the JVM reclaims it is not something you control
03

The cost side of the trade

Garbage collection is not free performance-wise — the JVM spends real CPU time finding and reclaiming eligible objects, and that work can pause your program briefly while it happens. A C program has no such pause, because there is no background process doing this work; the cost of memory management in C is entirely upfront, in the programmer's own discipline.

This is why Java is a poor fit for certain kinds of software — systems where an unpredictable pause of even a few milliseconds is unacceptable, like some real-time control systems — even though it is an excellent fit for the vast majority of applications, including everything you will build in this course. Automatic memory management is a genuine trade-off, not a strict improvement over what CS105ES taught you.

Try it yourself

Write three short statements describing a C program that leaks memory, a C program with a dangling pointer bug, and a Java program that makes an object eligible for garbage collection. Use the word "reach" or "reference" in each.

Need a hint?

A leak is memory nothing can reach that was never freed. A dangling pointer is a reference to memory that was freed while something still holds that reference.

Check the worked solution

C leak: memory that malloc reserved, that no pointer in the program can reach, and that was never freed. C dangling pointer: a pointer that still holds the address of memory that has already been freed, so using it reads or writes memory the program no longer owns. Java eligibility: an object that no variable, field, or array slot in the program can reach any more, marking it ready for the garbage collector, on its own schedule.

Quick check

Why can a Java program never have a dangling-pointer bug the way a C program can?

Select an answer to check your thinking.

Why this lesson exists

Syllabus mapping

Garbage collection.

Maps to course outcome CO1.