Object counts seem wrong (4.0.14)

Questions about YourKit Java Profiler
Post Reply
Gibson
Posts: 181
Joined: Mon Apr 11, 2005 10:38 am

Object counts seem wrong (4.0.14)

Post by Gibson »

I have the following code:

Code: Select all

import java.math.BigDecimal;
import com.yourkit.api.Controller;

public class MemoryTest {
	public static void main (String[] args) throws Exception {
		MT0 mt0 = new MT0 ();
		MT0 mt1 = new MT0 ();

		new Controller ().captureMemorySnapshot ("MemoryTest", false);
	}
}

class MT0 {
	Object ref1 = new Object ();
	Object ref2 = "Hello";
	Object ref3 = Integer.valueOf (10000);
	BigDecimal bd = new BigDecimal (0);
	int anInt;
}
When it's run, I would expect to see:
MT0
- ref1 2 Objects
- ref2 1 Object
- ref3 1 Object
- bd 2 Objects
However I see
MT0
- ref1 2 Objects
- ref2 2 Object
- ref3 2 Object
- bd 2 Objects

Is it a bug, or am I just misunderstanding the memory view?
Anton Katilin
Posts: 6172
Joined: Wed Aug 11, 2004 8:37 am

Post by Anton Katilin »

Try to hit F4 on e.g. ref2 line.

You'll see 2 objects - char[5] and String "Hello".

This field retains them both, although it refers directly to String only, which in its turn refers to char[] that holds contents of the string.

We do realize that this confuses. We're thinking about possible improvements of what the view shows for fields, to make the results more intuitive.
Gibson
Posts: 181
Joined: Mon Apr 11, 2005 10:38 am

Post by Gibson »

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?
Anton Katilin
Posts: 6172
Joined: Wed Aug 11, 2004 8:37 am

Post by Anton Katilin »

Please take a look at "bd" initialization - MT* object gets this field initialized with newly created BigDecimal:

Code: Select all

   BigDecimal bd = new BigDecimal (0);
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.
Gibson
Posts: 181
Joined: Mon Apr 11, 2005 10:38 am

Post by Gibson »

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"?
Anton Katilin
Posts: 6172
Joined: Wed Aug 11, 2004 8:37 am

Post by Anton Katilin »

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"?
You always see "deep number".
Gibson
Posts: 181
Joined: Mon Apr 11, 2005 10:38 am

Post by Gibson »

Well then, how come if I change my code to

Code: Select all

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 ... ?
Gibson
Posts: 181
Joined: Mon Apr 11, 2005 10:38 am

Post by Gibson »

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.

Thanks for your clarifications.
Post Reply