0

I am looking at a java virtual machine that's running an application. VisualVM says that java.lang.Float only has 102 instances.

Yet, there is also a class called Vector3 which has 80.000 instances. All these instances appear to have 3 float fields.

So, there should be at least 80.000 * 3 float instances right? Why not?

Update: This lead me to a follow up question, posted here: VisualVM OQL: how to search for primitive float values rather than actual Float instances?

Community
  • 1
  • 1
Tom
  • 8,536
  • 31
  • 133
  • 232

2 Answers2

3

float is a primitive value.

java.lang.Float is an object used to wrap up ('box') a float value into a referencable object.

A float field is not an instance of the boxed object. It's a primitive value.

It doesn't matter how many primitive fields you have - as they are not objects, they won't be counted in the object instance counts.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • Great, thanks a lot. However, this leads me to the question: how do I search for values of primitive values in VisualVM ? – Tom Feb 16 '11 at 20:47
  • I posted my follow up question here: http://stackoverflow.com/questions/5021988/visualvm-oql-how-to-search-for-primitive-float-values-rather-than-actual-float-i – Tom Feb 16 '11 at 20:56
1

To be precise, you have approximately 80.000 * 3 references to Float instances, but you have no guarantee that they are not referencing the same Float instances. Basically you have 102 Float instances, but each of them (or maybe just few) are referenced several times in different places.

Float instances, like all other primitive wrappers, are immutable, so there is nothing wrong with this.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • It seems very hard to believe that those 80.000 values have so many equal ones. This must be the case though... thanks. In fact, I am very sure that the app I am monitoring cannot suffice with only 120 different float values.. how weird. – Tom Feb 16 '11 at 20:27
  • @Tom, you can create an small app with something like this: `for( int i = 0 ; i < 121 ; i++ ) { list.add( new Float( i ) ); }` and confirm, you have +120 different objects. – OscarRyz Feb 16 '11 at 20:40
  • The difference is that I was expecting Float to include primitive float values. – Tom Feb 16 '11 at 20:47