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

Popular posts from this blog

php - How to pass multiple values from url -

database - php search bar when I press submit with nothing in the search bar it shows all the data -

ios - How to load .png images from Documents folder of an app -