java - What happens when weak referenced object is put itno HashMap as key? -
java - What happens when weak referenced object is put itno HashMap as key? -
this programme :
public class memoryleakswithmapsweakreference { private map<memoryleakswithmapsweakreference, integer> map = null; public memoryleakswithmapsweakreference() { map = new hashmap<memoryleakswithmapsweakreference, integer>(); } public static void main(string[] args) { memoryleakswithmapsweakreference m = new memoryleakswithmapsweakreference(); while(true) { m.run(); } } private void run() { memoryleakswithmapsweakreference mm = new memoryleakswithmapsweakreference(); weakreference<memoryleakswithmapsweakreference> ref = new weakreference<memoryleakswithmapsweakreference>(mm); mm = null; map.put(ref.get(), 1); system.out.println(map.size()); } }
now, if object weakreferenced, should deleted garbage collector. however, in case presented above (inserted map), not. can tell why map entries (weak reference) not getting destroyed ?
ref.get() returns null or hard reference object. map not contain 'weak' reference.
java garbage-collection
Comments
Post a Comment