How to clean objects (HashMap) for garbage collection - Java

So here I have a java program that manipulates a huge amount of data and stores it in objects (mainly hash maps). At some point in time, the data becomes useless, and I need to refuse so that I can free some memory.

My question is, what would be the best behavior to discard this data for garbage collection?

I tried map.clear (), however this is not enough to clear the memory allocated by the map.

EDIT (To add alternatives I tried)

I also tried system.gc () to get the garbage collector to start, however it didn't help

+4
source share
2 answers

HashMap # clear will remove all entries from the HashMap, but will not return it back to its original capacity . This means that you will have an empty support array with (in your case, I think) a space for tens of thousands of entries.

If you are not going to reuse HashMap (with approximately the same amount of data), just throw away the entire HashMap instance (set it to null).

In addition to the above:

  • if the Card entries still refer to any other part of your system, they will not be collected with garbage, even if they are removed from the Card (because they are needed elsewhere).
  • , . , , , .
+9
 system.gc() 

, jvm , . Class WeakHashMap<K,V> . , .

,

+4

All Articles