Do SoftReference
and WeakReference
really only help when created as instance variables? Is there any benefit to using them in method scope?
The other big part is ReferenceQueue
. Besides being able to track which references are determined garbage, can Reference.enqueue()
be used to forcibly register an object for garbage collection?
For example, would it be worth to create a method that takes some heavy memory resources (held by strong references) in an object and creating References to enqueue them?
Object bigObject;
public void dispose() {
ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
WeakReference<Object> ref = new WeakReference<Object>(bigObject, queue);
bigObject = null;
ref.enqueue();
}
(Imagine that Object in this case represents an object type that uses a lot of memory... like BufferedImage
or something)
Does this have any realistic effect? Or is this just a waste of code?
Not sure what is the question here but:
1) soft ref try to keep the reference until jvm really really needs the memory. Great for caches, esp LRU ones. Look at many examples in Guava.
2) weak ref don't try to prevent gc to free the object at all. They are used if you want to know if that object is still used somewhere. For example they are used to store info about threads and classes, so that when the thread or the class is not used anymore we can discard the metainfo related to that.
3) phantom ref are like weak but without letting you reference the actual object. In this way you can be sure that passing the phantom around cannot resume the actual object (this is a risk with weak ref). Also phantom ref are blocking the object to be collected until you clear the ref.
ReferenceQueue: you don't enque stuff in there. gc will do for you. They allows you to know when some references get released, without having to check them one by one.
If an object has only
WeakReference
s (or no references whatsoever!) towards it, it can be garbage collected whenever Java needs to make more room in memory. So, you useWeakReference
s whenever you want an object to remain in memory, but you don't need it to remain THAT badly (e.g. if Java needs to garbage collect it, no problem, you can get it back somehow and in the mean time Java has better performance)Enqueuing a
WeakReference
allows you to iterate theReferenceQueue
and determine which references have been garbage collected and which have not. That's all - so only do it if you need to know this.Read more: http://weblogs.java.net/blog/2006/05/04/understanding-weak-references
One common thing to do is to create maps of soft references.
I'm showing my old school here - the new java packages probably have much neater ways to do this.
One common idiom with reference queues is to e.g. subclass
WeakReference
to attach information that's needed to clean up things, and then to poll aReferenceQueue
to get cleanup tasks.For example, the internals of Guava's
CacheBuilder
implementation whenweakKeys
are selected uses this approach.