Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 46

Garbage Collection in Java

In this lesson: How Java automatically frees memory — heap, eligibility, System.gc(), generations — explained simply. In Hindi & English.

What is garbage collection

Garbage collection is Java automatically freeing memory that objects no longer need. In languages like C you free memory by hand, which is error-prone. In Java, the JVM's garbage collector finds objects nobody uses anymore and reclaims their memory for you.

💡 You never free memory by hand in Java — the garbage collector does it automatically.

Objects live on the heap

Every object you create with new lives on the heap — a region of memory the JVM manages. As your program runs, it makes many objects there. The garbage collector works on the heap, cleaning up objects that are no longer reachable to keep memory available.

When an object is collected

An object becomes eligible for collection when nothing references it anymore:

  • Its variable is set to null.
  • The variable goes out of scope (a method ends).
  • It is replaced by another object.

Once unreachable, the garbage collector may reclaim its memory — you do not control exactly when.

A simple example

String s = new String("Hello");   // object on the heap
s = null;                         // now unreachable
// the old "Hello" object is eligible for GC

Setting s = null removes the only reference to the "Hello" object, so nothing points to it. It is now eligible for garbage collection, and the JVM will free it when it runs the collector.

System.gc and finalize

System.gc();   // a SUGGESTION to run GC, not a command

System.gc() only asks the JVM to run garbage collection; the JVM decides whether and when. The old finalize() method (called before an object was collected) is deprecated — do not use it.

Why it helps

  • Fewer bugs — no forgetting to free memory, no double-frees.
  • No manual work — you focus on logic, not memory bookkeeping.
  • Safer programs — a big reason Java is reliable for large systems.

The trade-off is less precise control over exactly when memory is freed, which is fine for most programs.

Common mistakes

  • Thinking you must free memory. Java does it automatically.
  • Relying on System.gc(). It is only a suggestion, not a guarantee.
  • Using finalize(). It is deprecated and unreliable.
  • Keeping unwanted references. Objects still referenced are never collected — a memory leak.

Summary

  • Garbage collection frees memory of unused objects automatically.
  • Objects live on the heap, managed by the JVM.
  • An object is eligible when nothing references it anymore.
  • System.gc() is only a suggestion; finalize() is deprecated.
  • It reduces memory bugs — just drop references you no longer need.

Next, learn cleanup keywords in final, finally and finalize, or revise objects in classes and objects.

Frequently Asked Questions

What is garbage collection in Java?
Garbage collection is Java automatically finding and freeing memory used by objects that are no longer needed. Unlike languages such as C, you do not free memory by hand — the JVM's garbage collector does it for you.
When does an object become eligible for garbage collection?
An object becomes eligible when nothing references it anymore — for example, when you set its variable to null or it goes out of scope. Once unreachable, the garbage collector can reclaim its memory.
Where do Java objects live in memory?
Objects live on the heap, a region of memory managed by the JVM. The garbage collector works on the heap, removing objects that are no longer reachable to free space.
Can I force garbage collection in Java?
You can suggest it with System.gc(), but it is only a request — the JVM decides when to actually run. In practice you rarely call it; the garbage collector runs automatically as needed.
Do I need to free memory manually in Java?
No. Java manages memory automatically through garbage collection, which prevents many memory bugs common in manual-memory languages. You just stop referencing objects you no longer need.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicSerialization Your progress: 46 / 61 (75%) · Lesson 47 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free