How can you force garbage collection of an object?
gc()
. gc()
, passing in a reference to the object to be garbage-collected. gc()
. A.
Garbage collection cannot be forced.
B and D are incorrect. Calling System.gc()
or Runtime.gc()
is not 100 percent reliable, because the garbage-collection thread might defer to a thread of higher priority.
Option C is incorrect because the two gc()
methods do not take arguments.
If you still have a reference to pass into any method, the object is not yet eligible to be collected.
Option E will make the object eligible for collection the next time the garbage collector runs.