2

Possible Duplicate:
In Java, what is the best way to determine the size of an object?

In Actionscript I can usually just do:

var myVar:uint = 5;
getSize(myVar)
//outputs 4 bytes

How do I do this in Java?

Community
  • 1
  • 1
tetris11
  • 817
  • 2
  • 11
  • 27
  • thanks, I did see that before I asked - but I thought that Java might have it's own method. I dont feel like creating a whole new class for a quick query – tetris11 Nov 12 '11 at 13:51

3 Answers3

3

If you turn off -XX:-UseTLAB you can check the Runtime.freeMemory() before and after. However in the case of local variables, they don't take space on the heap (as they use the stack) and you can't get it size.

However, an int is a 32-bit sign value and you can expect it will use 4-bytes (or more depending on the JVM and the stack alignment etc)

The sizeof in C++ is useful for pointer arithmetic. Since Java doesn't allow this, its isn't useful and possibly deliberately hidden to avoid developers worrying about low level details.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Slightly out of my depth here. Could you provide an example? I dont really know how to turn off -XX:-UseTLAB? – tetris11 Nov 12 '11 at 13:52
  • Its a command line option, you add it to the command line before the class name. Basically, the size of the object is not something you should need to know. It may be interesting to you but I suggest you not worry about it unless you know you have a problem. In which cause I would use a profiler and that will give you the actual sizes. – Peter Lawrey Nov 12 '11 at 13:54
  • @PeterLawrey - See the linked question for a better way of doing this. – Stephen C Nov 12 '11 at 15:02
  • Using Instrumentation is better if you want to use this for more than experimentation. There are example of deep sizing of objects. IMHO, However the best way to do this is to use a profiler, and only worry about the things which are consuming the most memory. – Peter Lawrey Nov 12 '11 at 15:09
1

I believe there is no direct way of doing this. @Peter Lawrey 's suggestion could be a close approximation. But, you cannot rely on calculating the object size by taking the difference between the available free memory before and after the Object buildup, as there could be lots of other allocations in background happening from other threads as well. Also, there could be a possibility that the garbage collector could fire up and free up some memory in between your opertions. Also specially, in a multithreaded environment relying in the memory difference is definitely not a solution.

Drona
  • 6,886
  • 1
  • 29
  • 35
1

The only reason C had a sizeOf intrinsic (function? well something) was because they needed it for manual memory management and some pointer arithmetic stuff.

There's no need to have that in Java. Also how much memory an object takes up is completely implementation defined and can't be answered reliably, but you can try some statistics by allocating lots of the same object and averaging - this can work nicely if you observe some basic principles, but that's boring.

If we know some basics about our VM we can also just count memory, so for Hotspot:

  • 2 words overhead per object
  • every object is 8byte aligned (i.e. you have to round up to the next multiple of 8)
  • at least 1 word for variables, i.e. even if you have an object without any variables we "waste" 1 word

Also you should know your language spec a bit, so that you understand why an inner class has 1 additional reference than is obvious and why a static inner class does not.

A bit of work, but then it's generally a rather useless thing to know - if you're that worried about memory, you shouldn't be using neither ActionScript nor Java but C/C++ - you may get identical performance in Java, but you'll generally use about a factor of 2 more memory while doing so...

Voo
  • 29,040
  • 11
  • 82
  • 156