Emoticons On

In the following code example, when the keys are set to zero and System.gc() is WeakHashMap , WeakHashMap loses all mappings and is freed.

 class WeakHashMapExample { public static void main(String[] args) { Key k1 = new Key("Hello"); Key k2 = new Key("World"); Key k3 = new Key("Java"); Key k4 = new Key("Programming"); Map<Key, String> wm = new WeakHashMap<Key, String>(); wm.put(k1, "Hello"); wm.put(k2, "World"); wm.put(k3, "Java"); wm.put(k4, "Programming"); k1=null; k2=null; k3=null; k4=null; System.gc(); System.out.println("Weak Hash Map :"+wm.toString()); } } class Key{ private String key; public Key(String key) { this.key=key; } @Override public boolean equals(Object obj) { return this.key.equals((String)obj); } @Override public int hashCode() { return key.hashCode(); } @Override public String toString() { return key; } } 

Output: Weak Hash Map :{}

When WeakHashMap used with HashMap and the keys are set to null, WeakHashMap does not lose the mapping of key values.

 class WeakHashMapExample { public static void main(String[] args) { Key k1 = new Key("Hello"); Key k2 = new Key("World"); Key k3 = new Key("Java"); Key k4 = new Key("Programming"); Map<Key, String> wm = new WeakHashMap<Key, String>(); Map<Key, String> hm=new HashMap<Key, String>(); wm.put(k1, "Hello"); wm.put(k2, "World"); wm.put(k3, "Java"); wm.put(k4, "Programming"); hm.put(k1, "Hello"); hm.put(k2, "World"); hm.put(k3, "Java"); hm.put(k4, "Programming"); k1=null; k2=null; k3=null; k4=null; System.gc(); System.out.println("Weak Hash Map :"+wm.toString()); System.out.println("Hash Map :"+hm.toString()); } } class Key{ private String key; public Key(String key) { this.key=key; } @Override public boolean equals(Object obj) { return this.key.equals((String)obj); } @Override public int hashCode() { return key.hashCode(); } @Override public String toString() { return key; } } 

Conclusion: Weak Hash Map :{Java=Java, Hello=Hello, World=World, Programming=Programming} Hash Map :{Programming=Programming, World=World, Java=Java, Hello=Hello}

My question is: why WeakHashMap n't WeakHashMap lose its entries in the second code example even after the keys are discarded?

+10
source share
5 answers

WeakHashMap discards entries when the key is no longer available from live code. Because the HashMap maintains a hard link to keys, keys are still available, and WeakHashMap not a WeakHashMap entry.

The fact is that the behavior is associated with links to key objects, and not with the value of any variable that might once have a link to keys.

+16
source

The object must be dropped everywhere, and then WeakHashMap clears this object. Like WeakReference, its purpose is to remember an object if it is still in use. Without causing a memory leak, hold the object forever.

In your example, set hm = null; to see the magic of cleaning WeakHashMap.

+3
source

You set null to pointers k1,k2,k3,k4 but HashMap and WeakHashMap still contain references to these Keys . And since the HashMap contains a link, the actual key instances are not deleted by the GC. WeakHashMap still prints them all.

Try to run this example only with HashMap ->, even if you exclude these links, HashMap will still save them.

+3
source

HashMap dominates gc (garbage collector).

gc dominates WeakHashMap.

Despite the fact that we set the value null to k1, k2, k3, k4 gc, we do not delete from HashMap, where gc deletes them all and provides us with an empty map for WeakHashMap, therefore the name WeakHashMap

0
source

Try it -

 class WeakHashMapExample { public static void main(String[] args) { Key k1 = new Key("Hello"); Key k2 = new Key("World"); Key k3 = new Key("Java"); Key k4 = new Key("Programming"); Map<Key, String> hm=new HashMap<Key, String>(); hm.put(k1, "Hello"); hm.put(k2, "World"); hm.put(k3, "Java"); hm.put(k4, "Programming"); k1=null; k2=null; k3=null; k4=null; System.gc(); System.out.println("Hash Map :"+hm); System.out.println("Same thing with weakHash Map - "); k1 = new Key("Hello"); k2 = new Key("World"); k3 = new Key("Java"); k4 = new Key("Programming"); Map<Key, String> wm = new WeakHashMap<Key, String>(); wm.put(k1, "Hello"); wm.put(k2, "World"); wm.put(k3, "Java"); wm.put(k4, "Programming"); k1=null; k2=null; k3=null; k4=null; System.gc(); System.out.println("Weak Hash Map :"+wm); } } class Key{ private String key; public Key(String key) { this.key=key; } @Override public boolean equals(Object obj) { return this.key.equals((String)obj); } @Override public int hashCode() { return key.hashCode(); } @Override public String toString() { return key; } public void finalize() { System.out.println("Finalize method is called"); } } 
0
source

Source: https://habr.com/ru/post/954438/


All Articles