Possible Duplicate:
Size of a byte in memory – Java
I read this in an article. I am just pasting as is:
- The class takes up at least 8 bytes. So, if you say
**new Object();**you will allocate 8 bytes on the heap.- Each data member takes up 4 bytes, except for long and double which take up 8 bytes. Even if the data member is a byte, it will still take
up 4 bytes! In addition, the amount of memory used is increased in 8
byte blocks. So, if you have a class that contains one byte it will
take up 8 bytes for the class and 8 bytes for the data, totalling 16
bytes (groan!).
Is it true Java byte allocates / takes 4 bytes? and empty class takes 8 bytes?
Its confusing here as well.
That is false, at least for the HotSpot JVM. There, a primitive byte field in a class will take one byte, but it is true that the size of an instance of a class starts at 8 bytes, and fields are grouped into chunks that take eight bytes apiece — for example, you can’t split a field across an 8-byte boundary.
See e.g. http://www.codeinstructions.com/2008/12/java-objects-memory-structure.html