Newly, having the opportunity to study and use the following classes of the JAVA’s API: WeakHashMap, WeakReference, SoftReference, I would expose you some codes and explanations in this article which is composed by 2 sections.

1) WeakHashMap, WeakReference and Garbage collector
First, the two major differences between Java and previous languages low-level like C or C++ is that:

  • Java is an interpreted language, but will be executed “just-in-time” and compiled in a code understood by the microprocessor. The big advantage of this is that Java code can compile to execute with any operating system.
  • Java has a garbage collector that is responsible for recycling of memory previously allocated and unused. In C/C++, there is not the garbage collector and the programmer has the responsibility to deal with memory allocation. For more information read the good article on Garbage Collector How Garbage Collection works in Java.

However, even if the GC (garbage collector) has the responsability to remove an object/instance not used/referenced in memory, there are some situations for which, the GC can’t remove an instance. For example; if there is a set of objects:

Map<MyKey, MyObject> myHashMap = new HashMap<MyKey,MyObject>();
myHashMap .put(aKey, aObject);

Now, if the object ‘aObject’ is no longer useful, and if the programmer doesn’t delete it from the hashmap, this object ‘aObject’ will remain in memory because it is always referenced. In this case, the garbage collector does not release this object. The programmer must call the “remove()” method of map otherwise there will be a memory leak.

This is at this point that the WeakHashMap and WeakReference are very useful.
The WeakReferences can reference an object without “lock” on it for the garbage collector. In fact, a WeakReference object is referenced directly by the WeakHashMap, and the real key is referenced weakly from the WeakReference object. So, a WeakReference object is a kind of proxy around the real key.
This code demonstrates how to use the Java WeakHashMap class:

Map<MyKey, MyObject> myHashMap = new WeakHashMap<MyKey,MyObject>();
myHashMap.put(aKey, aObject);

Conceptually, this is similar to inserting a line before the “put()” call like this:

weakReferenkey = new WeakReferenkey(aKey);

The real key is referenced directly by the HashMap, but it is not referenced directly by the WeakHashMap.
So, there will not be memory leak because it means that if the “aObject” object is not referenced anywhere, it will disappear from your map from the passage of the GC.

2) SoftReference and Garbage collector
Some words about the SoftReference which is another type of reference to use in specific cases. These references are lower than WeakReferences because an object “bounded” by a SoftReference is eligible for GC only if the JVM is saturated. So, a WeakReference may disappear in the next GC execution, while a SoftReference remain in memory until saturation of JVM.

// Creation of a first SoftReference empty (of MyImage class) 
SoftReference<MyImage> softImage1 = new SoftReference<MyImage>(null); 
// Creation of a second SoftReference referenced a MyImage instance
MyImage aImage = new MyImage("C://.../././../me.jpg");
SoftReference<MyImage> softImage2 = new SoftReference<MyImage>(aImage); 

public Image getImage() throws IOException { 
    Image image = this.softImage1.get(); 
    if (image==null) { 
          // Load the image from IO
          image =; 
          // Create soft reference: 
          this.softImage1 = new SoftReference<Image>(image); 
    return image; 

In brief,

  • The SoftReference to manage the caches by allowing the removal if necessary.
  • The WeakReference are used to reference objects without preventing their losses.