Anshul
Anshul

Reputation: 121

Soft vs Weak References

I have a question on SoftReferences WeakReferences in Java.

What i know is:

GC uses algorithms to decide whether or not to reclaim a softly reachable object, but always reclaims a weakly reachable object.

Does that mean GC never runs the finalize() method on WeakReferences?

Thanks

Upvotes: 2

Views: 406

Answers (2)

Marko Topolnik
Marko Topolnik

Reputation: 200138

I don't follow your line of reasoning, but the finalizer is definitely always called. As soon as an object becomes finalizable, all soft/weak references to it will be cleared. So the refs can be observed as null before finalization.

Upvotes: 0

kosa
kosa

Reputation: 66637

As per The Truth About Garbage Collection

If a class defines a finalizer, then any instance of that class must have the finalizer called prior to deallocation. This means that deallocation is delayed by the inclusion of a finalizer.

Based on this my understanding is, irrespective of Week/Soft, if finalize defined, it will be called.

Upvotes: 2

Related Questions