I encountered some troubles with WeakHashMap.
Consider this sample code:
List<byte[]> list = new ArrayList<byte[]>();
Map<String, Calendar> map = new WeakHashMap<String, Calendar>();
String anObject = new String("string 1");
String anOtherObject = new String("string 2");
map.put(anObject, Calendar.getInstance());
map.put(anOtherObject, Calendar.getInstance());
// In order to test if the weakHashMap works, i remove the StrongReference in this object
anObject = null;
int i = 0;
while (map.size() == 2) {
byte[] tab = new byte[10000];
System.out.println("iteration " + i++ + "map size :" + map.size());
list.add(tab);
}
System.out.println("Map size " + map.size());
This code works. Inside the loops, i'm creating object.When a minor GC occurs, the map size is equal to 1 at the 1360th iteration. All is OK.
Now when i comment this line:
//anObject = null;
I expect to have an OutOfMemoryError because the mapSize is always equal to 2. However at the 26XXX th iteration, a full GC occurs and the map size is equal to 0. I dont understand why?
I thought that the map shouldn't have cleared because there are also strong references to both objects.