I read this article and now I'm clear about what is the basic difference between weak and soft references. I also understand that unlike weak references (which will cause the object to be collected in the next GC cycle if no strong references are there to that object), soft references are kept in memory until JVM runs out of memory.
But the question I have is if JVM runs out of memory and Garbage Collector starts collecting the soft references also, it should logically collect all the existing objects with soft references (my guess). Am I right about this?
Another thing that comes to my mind is that soft references have a very good use case, that is in-memory caches. But if all the soft references that I have will all be cleared in one single shot by GC, then it may not be the expected thing that we always want.
Is there a way to control the percent of soft-references that will be cleared? Or can we tell the GC to stop clearing the soft-references once it has recovered some specific amount of memory?
I'm asking this for the sake of getting a good grip on how soft references can be used effectively, and obviously no one will want their cache to be completely evicted from memory even when you need a very small amount of memory.