Q:

Application Memory not clearing in HashMap by remove() or removeAll() method

I have created a HashMap where millions of key insertions and removals happens continuously(in synchronized manner). However, removals does not remove the memory consumed by the HashMap. Due to this, my application's memory is going high and eventually at one moment, the application shuts down.
Can anyone suggest what should be the approach here to restore the memory upon removal of keys in HashMap.
Insertion:
map.put(txnId, new Timestamp(new Date().getTime()));

Removal:
Set<String> removalSet = new HashSet<String>();
removalSet.add(entry.getKey());

map.keySet().removeAll(removalSet);

A:

After the elements are removed make the reference of HashMap as null this would make this object eligible for GC. System.gc() behavior is not guaranteed and should be avoided.
The solution which worked out was:
Use Class WeakHashMap in this case. The objects will automatically be removed if the key is no longer valid refer.

