Weak and Soft References in Java
Consider a scenario where you have to build a finance based application that allows users to buy or sell assets in foreign currencies, in this case you will have to constantly fetch foreign currency exchange rates at every event the user triggers. Another common scenario will be to update exchange rates whenever they are updated in the real-world. Although finance applications use WebSockets to provide real-time data, for the context of this article we will exclude websockets. Let’s consider our scenario as a formal problem statement.
☹️Problem Statement: -
Develop a finance app that allows forex transactions.
Operations:-
buy
sell
updateExchangeRate
getExchangeRate
Challenges: -
At a large scale, if we rely on a database to store the data, the response time will be very high for each transaction and might overload the server with too many requests at a time.
If we rely on application storage, then the storage will be bloated with too much data.
If a currency exchange rate is rarely used but is eagerly loaded, will unnecessarily acquire space.
The first thing that will come to most of your minds is a Cache, which is correct but instead of relying on explicit cache providers we will try to understand a concept in Java that imitates a cache storage. In our context of a cache storage tracking foreign currency exchange rates, we cannot check which record / exchange rate is the least used manually, if a record is least used then ideally it should be evicted from the cache, instead of doing it manually, we can rely on JVM’s garbage collector to do that. But we cannot explicitly call the garbage collector and guarantee its execution. What if we can mark a variable such that it will be garbage collected when it has no references or when JVM is out of memory. This is when can introduce ourselves to Weak and Soft references
Soft References: -
A soft reference is an object which will be garbage collected when JVM is about to run out of memory.
Weak References: -
A weak reference is an object that is garbage collected when its referent object is no longer referred by any other reference.
Let’s look at some code to understand the definitions better.
We have declared two String objects and enclosed them in weak and soft reference type objects each. Take some time to go through the code and try to guess the output of the last two print statements.
Here is the output: -
The first print statement is the value of the weak reference and the second is for the soft reference. As per the definition, the weak reference enclosed object was garbage collected as soon as garbage collector ran. However, the soft reference held on to its value as JVM didn’t run out of memory. For the SoftReference object to be garbage collected, we will have emulate a situation where JVM is out of memory and is forced to remove the SoftReference object. Let’s look at some new code to demonstrate this: -
In this code we are allocating a SoftReference with a byte array of size 5MB and then declaring a List<byte[]>. Then we allocate blocks of 1MB to this List and print the SoftReference. Once JVM runs out of memory, we catch the error in the catch block, let’s execute this code with a slightly modified command to see garbage collection kick in soon.
On execution of these commands, we get the following output: -
We see that as long as JVM is able to hold on to the SoftReference, it will keep it in memory but as soon as it can’t it will remove it from the heap. JVM doesn’t wait until heap is full, whenever heap is about to be full, it will remove the SoftReference. This is how JVM takes are of SoftReferences.
So we now know that WeakReferences will be removed from the heap whenever the garbage collector runs irrespective of whether JVM is out of memory or not, and SoftReferences will be removed from the heap whenever the JVM is about to go out of memory.
Soft and Weak References have many applications, which we will cover in a different article.
If you notice any corrections in this article please feel free to list them down in the comments, as I novice writer I would love to improve upon my mistakes early.
Till then, happy coding!