Java Memory References — Java Programming level

In my previous posts, I have explained how Java garbage collector works internally and how effective it can be and where to fine-tune at JVM/collector level.

Here I am going to talk about how to interact with memory allocation from the Java application level. Please note, it is not always required for every application to use memory references to handle the effectiveness of garbage collection however it is desired to know about them in a little more details so when it comes to memory bottle-neck and/or working on a mega-scale transaction application, it will be much easier to know what to do and how to do.

As a Java programmer, we can make use of Java’s memory reference types to impact the underlying garbage collector and make an effect to improve the performance.

There are 3 types of memory references in Java

  1. Weak Reference

2. Soft Reference

3. Phantom Reference

The above are all Java classes and available for application programmers to use it directly.

Weak Reference

What?

A weak reference is a Java class WeakReference. It is a subclass of Reference class. It has two constructors for us to use. There are no local methods but it inherits methods from Reference and also obviously Object classes. It is used for to keep your object a little longer however, it will give up if there is a need for a new allocation to be occupied a memory space.

When?

There is a map called WeakHashMap which is not thread-safe. You can keep some information about an object until such time the object is cleaned. For example, using WeakHashMap, the each key is wrapped around WeakReference and when CG removes the key its value is also removed

Soft Reference

What?

A soft reference is a Java class SoftReference. It is a subclass of Reference class. It has two constructors for us to use and one method .get(). It also inherits methods from Reference and obviously Object classes. It is again keep your values up until there is a memory available in the heap. It will get cleared before OutOfMemory exception.

When?

You can use it when you want your application to store (caches) memory-intensive data. However, we have to be careful about keeping data for such a long time where you have a lot more to do with your business-critical application. You can use a Map and wrap your values with SoftReference so GC keeps your values up until there is memory available. You can use its .get() method to access the data.

Phantom Reference

What?

This is a bit different compare to Soft and Weak references. It stays on and GC normally doesn’t clean this but queue up. A phantom reference is a Java class PhantomReference. It is a subclass of Reference class. It has two constructors for us to use and one method .get(). It also inherits methods from Reference and obviously Object classes. You cannot access its referent directly that’s why GC puts this in reference queue after the finalise method of its referent is executed. It tells us that the data will be in memory!!

When?

There are at least two scenarios in which we can use this reference type.

  1. When we want to know when an object goes out-of-scope so we load another. For example, we don’t want to load a large object again before another large object is in memory
  2. You can avoid using finalise method and improve the finalise process itself


To view or add a comment, sign in

More articles by Murali R.

Explore content categories