开发者

Mixing different reference types in one collection

开发者 https://www.devze.com 2023-03-15 15:03 出处:网络
SoftReference, WeakReference, PhantomReference may be used to customize the process of garbage collection. All of them extend Reference<T> therefore it is possible to mix them in single collecti

SoftReference, WeakReference, PhantomReference may be used to customize the process of garbage collection. All of them extend Reference<T> therefore it is possible to mix them in single collection. Hard references (most common ones) do no extend Reference<T> therefore it is not possible to mix hard and other types of references in one collection. Am I right and we should put CustomReference<T> extends Reference<T> to the collection in order to achieve the desired result of mixing all types of object links in single collection (Collection<Reference<T>>)?

UPDATE: So when writing SSCCE I've found that it is not possible to extend Reference<T> in a usual way (constructor is package-local).

So the question now updates to the following: can I with single collection class create cache which always holds some objects (say 10) and the others are reclaimed by GC when memory not allows? Is there any other means to do this except providing custom wra开发者_开发问答ppers for hard and soft references and storing them in the collection?


Unfortunately Reference<T> most not (and can not) be subclassed directly, according to its JavaDoc:

Because reference objects are implemented in close cooperation with the garbage collector, this class may not be subclassed directly.

As such you won't be able to easily (i.e. without ugly instanceof + casting) handle both soft/weak/phantom references and normal references in the same Collection.

You could write a wrapper that either uses two separate Collection objects to handle normal and soft/weak/phantom references or that puts them all into the same Collection<Object> and uses the appropriate instanceof checks with casts to differentiate the objects.


You use a SoftReference when you want the referenced object to stay alive until the host process is running low on memory. The object will not be eligible for collection until the collector needs to free memory. Loosely stated, binding a SoftReference means, "Pin the object until you can't anymore."

By contrast, use a WeakReference when you don't want to influence the referenced object's lifetime; you merely want to make a separate assertion about the referenced object, so long as it remains alive. The object's eligibility for collection is not influenced by the presence of bound WeakReferences. Something like a an external mapping from object instance to related property, where the property need only be recorded so long as the related object is alive, is a good use for WeakReferences and WeakHashMap.

The last one -- PhantomReference -- is harder to characterize. Like WeakReference, such a bound PhantomReference exerts no influence on the referenced object's lifetime. But unlike the other reference types, one can't even dereference a PhantomReference. In a sense, it doesn't point to the thing it points to, as far as callers can tell. It merely allows one to associate some related data with the referenced object -- data that can later be inspected and acted upon when the PhantomReference gets queued in its related ReferenceQueue. Normally one derives a type from PhantomReference and includes some additional data in that derived type. Unfortunately, there's some downcasting involved to make use of such a derived type.

more info in this link

http://download.oracle.com/javase/7/docs/api/java/lang/ref/package-summary.html

Added one more link http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号