0

I want to properly free the memory associated with a HashMap. This answer suggests using clear()

Map.clear() vs new Map : Which one will be better?

Though, it does not take into account that clear() does not shrink the capacity to the original size. So, clearing a HashMap of 10k items, will leave behind the space of those 10k items.

On the other hand, this answer suggests using null and it takes into account the shrinkage and suggests that you use null instead of clear()

how to clear objects (HashMap) to be garbage collected - Java

I am using the HashMap to cache items on start and process them after some other processing. When I am done with them, I do not need them anymore. Though, if the user decides to perform the same operation (rarely happens), I use the HashMap again to cache the items and process them, then get rid of them.

I am bit confused. Which approach should I use?

Thanks.

Limmen
  • 45
  • 1
  • 6
  • 2
    '... clearing a `HashMap` of 10k items, will leave behind the space of those 10k items': no it won't. It will leave behind the space for 10,000 *references*`. – user207421 Jul 05 '20 at 10:09

2 Answers2

1

As mentioned here (which you went through),

Setting the Map to null will do the work. But remember that garbage collection happens in the background, and only when it is required. So you may not immediately see a lot of memory being freed.

You can even take a look at WeakHashMap here. It's keys are 'Weakreference'-d hence garbage collected eagerly.

WeakHashMap is ideal for a scenario like this: (Quoting from here)

Let's say that we want to build a cache that keeps big image objects as values, and image names as keys. We want to pick a proper map implementation for solving that problem.

Using a simple HashMap will not be a good choice because the value objects may occupy a lot of memory. What's more, they'll never be reclaimed from the cache by a GC process, even when they are not in use in our application anymore.

Ideally, we want a Map implementation that allows GC to automatically delete unused objects.

Abhinaba Chakraborty
  • 3,488
  • 2
  • 16
  • 37
  • Thanks for the information. I do not really understand how WeakHashMap works. I cache the values during the operation. Then, around a minute later, I iterate over the HashMap items. I decided to go with the `null` approach using HashMap. – Limmen Jul 06 '20 at 08:46
  • 1
    A name typically is something that can be reconstructed, like a string. Using weak references therefore is a very bad idea, as when a name becomes unreachable, the value might get removed from the `WeakHashMap` while there are lookups with an equal key later on. For a cache, you either use an ordinary map or soft references (there’s no built-in map type with soft keys). – Holger Aug 25 '22 at 17:17
1

For your usecase i think using WeakHashMap is best. This is very suitable for you because you don't need the hashmap for long time. so weak reference is better than a strong referenced HashMap.

WeakHashMap<String, String> map = new WeakHashMap<>();
    map.put("Hello", "test");
Onirban
  • 146
  • 6
  • calling System.gc() is considered as bad practice unless you are at a terrible state of memory usage. – Abhinaba Chakraborty Jul 05 '20 at 09:51
  • 1
    its just an example for testing how this WeakHashMap works. without calling the gc here, how you will know when the gc will collect your reference? – Onirban Jul 05 '20 at 09:55
  • Even if you do call `System.gc()` you don't know that the GC will collect your reference. Seriously, just don't do it. You will give someone the wrong idea that *they* should do it. – Stephen C Jul 05 '20 at 11:47
  • 1
    The string `"Hello"` will never be garbage collected, so it’s pointless to use a `WeakHashMap` with such a key. – Holger Aug 25 '22 at 17:09