• 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

Soft Hashmap, weak, strong references etc.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please put me on the right track with this?. I've had a search through the forums and google and can't find anything relevant, but apologies if I'm repeating a thread. Anyway, on to the question.

Basically I'm throwing around some pretty large hashmaps and to avoid memory problems I'd like to make it a soft hashmap. Now, it's my understanding that the softness means if an element of the hashmap is not strongly referenced it can be garbage collected. However, if the key of a gc'd element is then accessed (using get or put), the element is reloaded into memory from a cache on a hardrive. Is this even remotely correct?

Thanks for any help in advance. Life has been saved by this site quite a few times, but this is my first question.

Cheers
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you're asking about something which doesn't actually exist in the standard API. So it's a hypothetical question. If you wrote a SoftHashMap class, you could certainly write it so that it reloaded entries which had been garbage-collected from a cache, or from the database they originally came from, or something like that.

However all you have to work with is SoftReference(object). And all you can get out of that (via the get()) method is the object (if it hasn't yet been garbage-collected) or null (if it has).
 
Adam Rees
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

Thanks for your prompt reply. Digging further into the hierarchy, you're absolutly right. I didn't realise AbstractSoftMap was one of our classes. Sorry.

Thanks again
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wanted to clarify: (making an analogy from WeakHashMap though)

A SoftHashMap, if made on the lines of WeakHashMap, will have the keys as soft references and not the values.

(Since this is not really related to performance, moving to Java In General forum)
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Adam Rees wrote:Basically I'm throwing around some pretty large hashmaps and to avoid memory problems I'd like to make it a soft hashmap. Now, it's my understanding that the softness means if an element of the hashmap is not strongly referenced it can be garbage collected. However, if the key of a gc'd element is then accessed (using get or put), the element is reloaded into memory from a cache on a hardrive. Is this even remotely correct?



If you're talking about WeakHashMap in the java.util package then they way it works is that an entry may disappear when its key is not strongly referenced. It may be that the entry was the only strong reference to the value, and if so the value becomes eligible for GC.

Whether they are GC'd or not, they are gone from the map. There is no memory or disk cache (unless you've implemented your own somehow) and nothing is reloaded.

You may also want to take a look at LinkedHashMap. It has nothing to do with weak references, but it has methods that can be overridden to remove old entries as new ones are added so that the number of mappings doesn't get too large. They can be used to essentially turn LinkedHashMap into an LRU cache.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic