Wednesday, January 29, 2014

WeakReference example

WeakReference is a great mechanism.
Usual references are called Strong References.
For example,
string referenceToString = new string("The string!");

Garbage Collector collects and destroys the object if there is no any Strong reference to it.

string referenceToString = null;
If there is no strong reference to "The string!" then it will be collected by garbage collector and destroyed.

WeakReference is a wrapper on top of a Strong reference with one core distinction "WeakReference does not protect from garbage collection".

string referenceToString = new string("The string!");
var wr = new WeakReference(referenceToString);
referenceToString = null;

// some execution time left
// here wr can point to: 
// 1) NULLL if it was garbage collected or
// 2) object "The string!" if it was not garbage collected

var wrTarget = wr.Target as string; // return internal object of weak reference

Usually weak references are used wrong. Take the dialog from my old interview:
Interviewer: What is WeakReference?
Me: I did not know.
Interviewer: (answers the definition)
Me: But where it can be applied?
Interviewer: Mmmm... for example, in a cache. You can have very heavy resources, and resource can be recreated after WeakReference internal object will be garbage collected.


It is very hard to find good example of proper usage for WeakReference. Why on the earth cache mechanism should depend on Garbage Collection time? There are lots of customisable and convenient solutions for caching.

One of the great example was found for Android development (Java).
UI thread can be locked if the image will be loaded not from memory (disk, net). This can be solved by creating asynchronous task that will load the image. The problem arises if user goes out from current page of the application or Android unloads invisible part of the page. Then the image container on the page (ImageView) will not be needed at all when async operation finishes. This can be smartly solved with WeakReference:

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<ImageView> imageViewReference;

    public BitmapWorkerTask(ImageView imageView) {
        imageViewReference = new WeakReference<ImageView>(imageView);
    }
    // Method for getting bitmap is removed for code clearness

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}
Now image container can be garbage collected.

1 comment: