Search This Blog

Showing posts with label soft reference. Show all posts
Showing posts with label soft reference. Show all posts

Wednesday, June 15, 2011

Weak Reference in Java

Today, during a phone interview, I was asked a question about WeakReference in Java.  I only knew that the weak referenced object can be garbage collected by JVM, and nothing more.  I did a little research, and realized that there are 4 types of references in Java:
  1. Strong Reference
  2. Soft Reference
  3. Weak Reference
  4. Phantom Reference
They are in the order from strong to weak references.  3 classes, SoftReference, WeakReference, and PhantomReference are for the last 3 references.  For strong reference, just use new operator. Below is a great blog talking about all 4 references:

Understanding Weak References