• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can we Use SoftReference in Java for better performance

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 83
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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$
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should also be looking at WeakHashMaps
 
reply
    Bookmark Topic Watch Topic
  • New Topic