Java Reference
In-Depth Information
// Print a message
printMessage("After second gc() call:", wmap);
/* Both keys have been reclaimed by now. Just to make value
objects reclaimable, we will call clear() method on WeakHashMap.
Usually, you will not call this method here in your program.
*/
wmap.clear();
// Invoke gc() so that value object will be reclaimed
System.out.println("Invoking gc() third time...");
System.gc();
// Print message
printMessage("After calling clear() method:", wmap);
}
public static void printMessage(String msgHeader, WeakHashMap wmap){
System.out.println(msgHeader) ;
// Print the size and content of map */
System.out.println("Size=" + wmap.size());
System.out.println("Content=" + wmap);
System.out.println();
}
}
After adding two entries:
Size=2
Content={BigObject: id = 10=BigObject: id = 110, BigObject: id = 20=BigObject: id = 210}
Invoking gc() first time...
After first gc() call:
Size=2
Content={BigObject: id = 10=BigObject: id = 110, BigObject: id = 20=BigObject: id = 210}
Invoking gc() second time...
After second gc() call:
finalize() called for id:20
finalize() called for id:10
Size=0
Content={}
Invoking gc() third time...
After calling clear() method:
finalize() called for id:210
finalize() called for id:110
Size=0
Content={}
 
Search WWH ::




Custom Search