0

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

What is the technique to retrieve the # of bytes occupied by a class object/variable in Java?

Looking for an equivalent for SizeOf() operator in C.

Community
  • 1
  • 1
Aditya369
  • 545
  • 1
  • 7
  • 27
  • 9
    Googling 'java sizeof' turns up hundreds of hits - did none of them help you? – Carl Norum Sep 27 '11 at 16:43
  • 3
    http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object – fazo Sep 27 '11 at 16:45
  • 1
    Ditto. Look for example [here](http://www.glenmccl.com/tip_038.htm). – Luca Geretti Sep 27 '11 at 16:46
  • 1
    `sizeof` in C is meaningful in C only due to the "representation of types", i.e. the fact that each object is represented as an overlaid array of `unsigned char [sizeof(type)]`. This representation does not exist in Java, and moreover I think a large part of the point of Java is to hide all its bloat in ways you don't notice, a goal to which revealing the memory usage of objects would be counterproductive. See also http://domino.watson.ibm.com/comm/research_people.nsf/pages/nickmitchell.pubs.html/$FILE/oopsla2007-bloat.pdf – R.. GitHub STOP HELPING ICE Sep 27 '11 at 17:13
  • @CarlNorum, hundreds? I get over one million results. ;) – Peter Lawrey Sep 27 '11 at 17:57

1 Answers1

4

I've used the SizeOf() project on Sourceforge before with decent success:

http://sourceforge.net/projects/sizeof/

However, based on how the JVM works, Sizeof for java is, at best, an intelligent guess based on predefined parameters rather than actually measuring the size of the memory allocation.

...so use this as a source of information rather than gospel!

claymore1977
  • 1,385
  • 7
  • 12