Junior — Middle
How does the JVM determine that the object referenced by a WeakReference can be garbage collected?
sobes.tech AI
Answer from AI
The JVM uses the mechanism of weak references (WeakReference) to allow the garbage collector to delete objects that are only referenced by weak references. If an object has no strong references, the next garbage collection cycle considers it eligible for deletion, even if there is a WeakReference to it. Thus, the JVM checks for the presence of strong references to the object — if none are found, the object is considered unreachable and can be deleted, and the WeakReference is cleared (set to null).