Multiple choice technology programming languages

How can you force garbage collection of an object in Java?

  1. Garbage collection cannot be forced

  2. Call System.gc()

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

  4. Call Runtime.gc()

  5. Set all references to the object to new values (null, for example)

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

In Java, garbage collection is managed entirely by the JVM and cannot be forced. Calling System.gc() or Runtime.getRuntime().gc() only suggests or requests that the JVM run the garbage collector, but does not guarantee or force its immediate execution.