Multiple choice technology architecture

How can you force garbage collection of an object?

  1. Call System.gc()

  2. Garbage collection cannot be forced.

  3. Call System.gc(), passing in a reference to the object to be garbage-collected.

  4. Call Runtime.gc()

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Java garbage collection cannot be forced - calling System.gc() or Runtime.gc() only suggests to the JVM that garbage collection should run, but the JVM may ignore the request. Garbage collection runs automatically based on heap pressure and JVM algorithms. This is a fundamental Java concept developers must understand.