I am testing SoftReference for a cache implementation and I found a strange behaviour :
I have a setName(String name)
method which sets the name of a Graph object through a SoftReference :
public void setName(String newName) {
getData().name = new SoftReference<String>(newName,garbagedData);
}
(garbagedData is a ReferenceQueue and doesn't seem important in this particular problem).
When I call graph.setName("name");
from main thread, and when à force a OutOfMemory
error, the value pointed by the reference is not garbaged but if I call graph.setName(new String("name"))
then it is.
I look the heap contents with Eclipse Memory Analyzer and in both case there is no other reference chain than the Soft one.
If anyone has an explanation for this strange behavior I am interested.