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

You cannot force garbage collection in Java - you can only suggest it using System.gc() or Runtime.gc(). The JVM makes the final decision based on memory pressure and its own optimization strategies. Even when gc() runs, it may not collect all eligible objects. This design allows for better performance tuning.