As you may already know objects in Java are created on the heap. But are all objects equally strong? Can some of them be knocked out of the heap without their knowledge? The answer to the latter -- maybe surprisingly -- is yes. Objects can indeed be knocked out of the heap without their knowledge even when they are not candidates for garbage collection. These objects are perfectly good objects which are being referenced and used, but they can still be removed from the heap. Not because they are hacked, but because they are weak references.
When you write the following code:
A Widget object is created on the heap, and the variable w holds a reference to that object. As long as the variable w is active, the object which it points to (Widget) will not be garbage collected.
Every Java developer is familiar with this kind of a reference, and even though we rarely use a label to identify it, such a reference is a strong reference. Strong because the object referenced by it cannot be garbage collected against its will. However, Java also has an alternate type of reference - a weak reference. Objects referenced by them may be garbage collected -- if the JVM runs short of memory -- even when they are being used. Creating a weak reference is a way of telling the JVM:
Look dude, I am creating this object as a weak reference. Even though I need it, feel free to garbage collect it if you run out of memory. I know this object can be GC'd any time and am prepared to deal with it.
So how do we create a weak reference? Simple! We use Java's WeakReference class - like the code below which creates a weak reference to the String "abc":
The interesting thing about code sample 3 is, we can never assume that the object we are trying to get actually exists. It's a little akin asking for a discount; if you get it, awesome; if not, tough luck, try elsewhere. The most common use for weak references are read-only caches, where losing an object is not disastrous. It just means we have to recreate the object, usually back from the database.
Here's how you would create a simple cache of Widget objects using weak references:
If you ever find yourself writing caches such as this, first smack your knuckles for not having looked up the JDK's classes for an existing solution and then take a look at the WeakHashMap class - it does exactly what you need.