user1033098
user1033098

Reputation: 205

Why weakreference object can be recreated easily?

MSDN:

Weak references are useful for objects that use a lot of memory, but can be recreated easily if they are reclaimed by garbage collection.

and

When you use a weak reference, the application can still obtain a strong reference to the object, which prevents it from being collected. However, there is always the risk that the garbage collector will get to the object first before a strong reference is reestablished.

My question is: Why can a WeakReference object be recreated easily? After the GC releases it, can be recreated easily, compared to normal objects?

Upvotes: 1

Views: 249

Answers (3)

supercat
supercat

Reputation: 81159

The primary correct use of weak references is in situations where the deciding factor for whether a reference is likely to be useful is whether someone else holds a reference to the same thing. Two common examples of such situations:

  1. An object Foo may hold a weak reference to Bar if it is expected to update or manipulate Bar in some fashion *for the benefit of other objects*, but it would just as soon not bother if nobody cares whether Bar gets updated or not. If objects which really care about Bar hold strong references to it, while those that don't care about it hold weak references, then once nobody cares about Bar, it will become eligible for collection, the weak references will be invalidated, and the objects that hold such references will find that they no longer have to deal with Bar.
  2. If an object builds a large immutable object and discovers that it precisely matches one to which a reference already exists, it may be helpful to abandon the newly-built object and substitute a reference to the latter one. This will save memory, and may also greatly expedite comparisons (comparing two distinct large objects which happen to be identical may be expensive, but comparing two references to the same large object is very cheap). It's helpful to cache a reference to the large object as long as someone else needs it anyway, but keeping the object in cache when no other reference exists would actually be counter-productive (if nothing else holds a reference to the cached copy, there would be little or no advantage to abandoning the new copy and keeping the cached one, versus abandoning the cached copy and keeping the new one; consequently comparing the new copy to the cached one to find out whether one could use the cached one adn abandon the new would be a waste of time).

In deciding whether to use a WeakReference, one should decide whether one would be happy if the WeakReference got invalidated the moment no other reference existed to its target. If one would want WeakReference to remain valid for awhile, that's a sign that one should probably use some other caching mechanism.

Upvotes: 0

AakashM
AakashM

Reputation: 63338

You're misreading the text. When it says

Weak references are useful for objects that use a lot of memory, but can be recreated easily if they are reclaimed by garbage collection.

it means

IF

you have an object that uses a lot of memory but could be easily recreated

THEN

a weak reference to this object could be useful

Upvotes: 5

João Angelo
João Angelo

Reputation: 57688

There is no difference, the MSDN documentation is trying to describe the use case for weak references, that is, weak references should be used associated with objects that use a lot of memory but at the same time are easily to be recreated.

A weak reference does not prevent an object from being garbage collected, so an object that is holding a lot of memory may be collected so that the memory can be reused. However if the object that was collected is expensive to recreate the benefits of using a weak reference and letting the object be garbage collected is lost when it comes time to use the object and a recreation is required because the object was collected.

Upvotes: 3

Related Questions