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.