Reputation: 1665
I have a Map
Map<OuterKey,Map<InnerKey,Object>> sampleMap
on this I do sampleMap.remove(OuterKey)
Now what does this imply to the inner map? would it be garbage collected on the next cycle?
Do you guys recommend me doing
Map<InnerKey,Object> innerMap = sampleMap.remove(OuterKey);
innerMap.clear();
Does this help from the perspective of GC?
Thanks
Upvotes: 0
Views: 155
Reputation: 13930
When there are no more references to the object it will be marked for garbage collection and it's then the gc's responsibility. It doesn't guarantee to automatically run. So clearing it wouldn't achieve anything.
That is for two reasons.
innerMap
) to it. If anything that hinders gc until you get rid of that one too.Upvotes: 1
Reputation: 12857
GC only cares about references to your object, and no GC is not helped by clearing the inner map.
In your example sampleMap
had the only reference to the innerMap
, so when you removed the entry from sampleMap
there would be no other refs to your innerMap (after your local variable innerMap
goes out of scope), so you have dereferenced
the innerMap.
GC would mark your innerMap as eligible for finalization, if a finalizer was coded it would be run. After that you can consider your object GC'd.
Of course when does GC run? It's not something you can predict outside of brute force calling it (via GC.Collect()
) and that should only be done in rare/unique circumstances.
Upvotes: 1