I have some HashMap < String, XYZ > created having the key type as String and value type of some XYZ Class.Now for some condition check I want to free/ garbage collect the entire HashMap along with the individual objects of XYZ type that it holds. Is it possible to do it without iterating the hash map and setting each object to null ? I guess by setting only HashMap to null will not work. How do I ensure later that the collection of objects is garbage collected ?
If I do something like:
XYZ obj = new XYZ();
hm.put("<unique-id>",obj);
XYZ obj2 = new XYZ();
hm.put("<unique-id>",obj2);
.
.
.<more operations>
.
.
hm = null;
my code no longer reference either obj or obj2 after hm=null statement.
So can I be sure enough that the jvm will garbage collect obj and obj2 also shortly ?
Moreover in cases where hashmap is a huge collection, I dont want to use Iterator over HashMap to get and iterate over every object and set it to null.
Does just hm=null does the above stuffs of GCing the entire collection of objects + map for me ?
hashMap = nullis enough if nothing else references the keys / values. - zapl