Weak references and garbage collection?

Yes it can, that is exactly how weak references are designed to work. The weak reference is the root that your object has to the application, even though the object may have other strong references it is the root reference that matters and since the root reference is a weak reference the object will be a candidate for garbage collection For more information please see WeakReference : Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable.At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references.

At the same time it will declare all of the formerly weakly-reachable objects to be finalizable. At the same time or at some later time it will enqueue those newly-cleared weak references that are registered with reference queues.

Yes it can, that is exactly how weak references are designed to work. The weak reference is the root that your object has to the application, even though the object may have other strong references it is the root reference that matters and since the root reference is a weak reference the object will be a candidate for garbage collection. For more information please see WeakReference: Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed.

Weak references are most often used to implement canonicalizing mappings. Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable. At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references.At the same time it will declare all of the formerly weakly-reachable objects to be finalizable.

At the same time or at some later time it will enqueue those newly-cleared weak references that are registered with reference queues.

I've never heard the expression weak reference and it sounds like it implies some mecanism. If I understand things correctly, the question is whether engine will be garbage collected if nothing is referring car? – James Poulson Apr 6 '10 at 12:26 @James P.

: WeakReference is a class in the Java standard API that indeed provides a special mechanism, namely references that do not prevent an object from being eligible for garbage collection. – Michael Borgwardt Apr 6 '10 at 12:48.

The Car instance could be garbage collected, but there is no guarantee that it will be garbage collected on the next GC cycle, or even that it will be collected at all. For example, At some time before the GC runs, the application could call get on the WeakReference and save the reference to the Car in (for example) an attribute of some reachable object. The Car instance then becomes fully reachable and no longer eligible for garbage collection.

If the GC runs with the Car in the state described, the JVM spec does not guaranteed that the weakly reachable will be detected in the next GC cycle. For example, if a given GC cycle only collects the latest generation (and the Car has been promoted to an older generation) the GC won't determine that it is weakly reachable. Even when the GC breaks reference to the Car in the WeakReference, the Car instance is not reclaimed immediately.

Rather, the reclamation of the now unreachable Car will probably happen (after possible finalization) in a later GC cycle.

Here's a unit test that demonstrates weak references. Note that System.gc() will not guarantee that the object will get garbage collected and you should not rely on it. Import junit.framework.

TestCase; import java.lang.ref. WeakReference; public class WeakReferenceTest extends TestCase { class Car { Engine engine = new Engine(); } class Engine { } public void testWeakReferences() { WeakReference carRef = new WeakReference(new Car()); assertNotNull(carRef.get()); System.gc(); assertNull(carRef.get()); } }.

– James Poulson Apr 6 '10 at 12:33 4 That call to System.gc() doesn't guarantee that the last assert will return true. – Phil Apr 6 '10 at 12:39 It is a parameterization of the generic class WeakReference in which the type parameter T is the class Car. – Stephen C Apr 6 '10 at 12:41 The System.gc() will indeed not guarantee that the object will get garbage collected, and you should not rely on it.

– Frederik Apr 6 '10 at 13:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions