4

I was going through WeakHashMap in Java. And what I have understood is WeakHashMap is exactly as HashMap except its key references are WeakReference. That means key references are eligible for gc, and when it is garbaged, its entry will be removed from the map. This is not available in HashMap. Please correct me if I am wrong.

I have one question over here.

Now in future If I get a requirement that I have to use Map for putting key and value,can I go forward with WeakHashMap? Or do I need to consider any scenario where WeakHashMap wont be fit only HashMap will fit?

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
user414967
  • 5,225
  • 10
  • 40
  • 61
  • 3
    Do you understand what WeakReference means? And when exactly it will become eligible for GC? You cannot just replace all HashMaps with WeakHashMaps... They both have different behaviors... – Codebender Oct 10 '15 at 03:45
  • 6
    See also [When would you use a WeakHashMap or a WeakReference?](http://stackoverflow.com/questions/154724/when-would-you-use-a-weakhashmap-or-a-weakreference) and [What is a WeakHashMap and when to use it?](http://stackoverflow.com/questions/5511279/what-is-a-weakhashmap-and-when-to-use-it) – Mick Mnemonic Oct 10 '15 at 03:47

2 Answers2

2

You do need to consider the context to decide whether it is correct / safe to use a WeakHashMap.

Here's an example where a WeakHashMap would not work (pseudo-code)

Map<Name, Details> map = ...
do for ever:
    name = get name from user
    if lookup:
        details = map.get(name)
        display details
    else if create:
        details = get details from user
        map.add(name, details)

With a WeakHashMap, there is a risk that entries will drop out of the table, and the user's lookups will fail. With a HashMap, there is no risk.


There is also the issue that WeakReference and anything built on it is more expensive than ordinary references. They use more space and time. More significantly, the Reference classes incur overheads each time the GC encounters them, which can increase GC pause times.

However, the issue of runtime overheads should normally be secondary to the correctness issue.

  • If you use a HashMap when you should be using a WeakHashMap, then you are liable to run into problems with your heap filling up. And that has performance issues too.

  • If you use WeakHashMap where you should be using a HashMap, you may loose information.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

There are cases where a WeakHashMap won't replace a HashMap, for example:

  1. When calling an API where an argument type is the concrete type HashMap<K, V> rather than the interface type Map<K, V>.
  2. When your code assumes nothing else is changing the map out from under it. The garbage collector can remove weak keys at any time, causing the WeakHashMap methods to act as if entries were removed just then. So e.g. if you put the map size into a variable, the actual map may be smaller than that when you enumerate it. (See the JavaDoc for examples.) It's risky to pass a WeakHashMap to code that may not be prepared for such changes. Synchronizing won't help this.
  3. When you don't want the extra overhead in space and time.

I recommend using a WeakHashMap only when you need to (e.g. a listener registry) and only when all the code that touches it is explicitly prepared for disappearing entries.

Jerry101
  • 12,157
  • 5
  • 44
  • 63