Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can we Use SoftReference in Java for better performance  RSS feed

 
Priyanka Dandekar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have heard that with the use of SoftReference we can build faster and efficient cache.

I have no clue how it can be done, We are trying to build a HashMap based cache, I am just thinking of utilizing SoftReference based cache to have memory efficient implementation.

Can someone please suggest how I can do it?

Any example code or links would be really helpful.

Also let me know is it really worth doing ? Or a simple HashMap based cache is better to use?
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SoftReference are good way to implement memory efficient cache as the Java Specification says that Garbage collector will clear all soft references before throwing OutofMemoryError.

Code will definitely become more complex then a simple HashMap cache, so if you are going to build a really memory consuming cache then you can utilize a SoftReference based cache.

Here is an example of [ Cache Using SoftReference ]

There are more ways to implement reference queue clean up logic. May be other people on forum can also give input on the same.
 
Sathya Sayee
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a good reason not to use something readily available. I am pretty sure a cache like ehcache would give you all that you need.

my 0.02$
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sathya Sayee:
Is there a good reason not to use something readily available. I am pretty sure a cache like ehcache would give you all that you need.

my 0.02$


Well, "cache" is just a generic term here. It could just be something small like a map of listeners -- and using a full blown cache is just an overkill.

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note:

I actually had a debate about soft and weak references, with a colleague. And I think I lost.

My argument was soft references was a good way to let Java keep a "cache" of Least Recently Used items. The counter argument was that, it is highly unlikely there is one "cache" in the program. Instead, there should be many many small ones. Unfortunately, since Java only has one set of LRUs, mini caches with low-turnover will always be empty, as they are at the mercy of mini-caches with high turnover.

So, instead of soft references, it is probably better to use strong references for a minimum amount of data, and change them over to weak references, when they get old.

Henry
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
My argument was soft references was a good way to let Java keep a "cache" of Least Recently Used items.


Of course, while LRU is commonly used, is not close to optimal. Much better to use working sets. Some java code to implement them is in

http://www.pfarrell.com/java/
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also be looking at WeakHashMaps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!