I am reading about Weak References.
I am using the code to study from here. It is very simple.
private void doFunction() throws InterruptedException {
Map<Integer, String> map = new HashMap<Integer, String>();
myMap = new WeakReference<Map<Integer, String>>(map);
map = null;
int i = 0;
while (true) {
if (myMap != null && myMap.get() != null) {
myMap.get().put(i++, "test" + i);
System.out.println("im still working!!!!");
}
else {
System.out.println("*******im free at:"+i+"*******");
Thread.sleep(5000);
if(myMap != null){
System.out.println("*******myMap is not null*******");
}
}
}
I did not request a small heap size or any size via –Xms and –Xmx
but was able to see the values removed from the cache when i == 15312
.
So after 15312
objects in the Map
the GC starts to remove entries.
My question: Isn't 15312
too low for a 32 bit machine
with 4 GB
of memory? I was expecting a much higher value before the references are starting to be removed.
Am I wrong on this? How can one evaluate at which points the objects will start to be removed?