OK, now I'm really confused!
I have ref2 which references 2 Objects - when I F4 on it, sure enough I see an [A] char[6] and a [O] String "Hello"
But my bd (BigDecimal) references 2 objects, and the intVal references that they contain aren't counted with the object count ... are you just saying that there is special treatment for the case of String objects?
ref2 is initialized with string literal, which is interned by the JVM, in other words, all strings written inside your Java program as "Hello" will actually mean a singleton object.
So you initialize ref2 twice (for MT0 and MT1) with the same object.
Things will be different if you change ref2 initialization with e.g. new String("Hello")
Last edited by Anton Katilin on Mon Jul 18, 2005 2:14 pm, edited 1 time in total.
In other words, is it right to say: for String objects, I am seeing the "deep number of retained objects", whereas for objects of other classes, I am just seeing the "shallow number of retained objects"?
In other words, is it right to say: for String objects, I am seeing the "deep number of retained objects", whereas for objects of other classes, I am just seeing the "shallow number of retained objects"?
public class MemoryTest {
public static void main (String[] args) throws Exception {
MT0 mt0 = new MT0 ();
MT0 mt1 = new MT0 ();
MT0 mt2 = new MT0 ();
new Controller ().captureMemorySnapshot ("MemoryTest", false);
}
}
class MT0 {
BigDecimal bd = new BigDecimal (0);
}
then my All Objects class tree shows 3 objects for the field bd. That is the "shallow" count for me, since each BigDecimal object contains a BigInteger, and each BigInteger itself contains an int[], so "deep" count should be 9 ... ?
Oops, sorry, got it. The case of new BigDecimal(0) is handled specially internally, so the BigInteger object and corresponding int[] is shared, plus it is referenced intenally in java.math and hence it is not "retained" by any of my classes. If I change the BigDecimal to have value 123 I get a retained count of 9, like I would expect.