Tuesday, April 20, 2010

Memory Structure of Java Object

"Every object is aligned to an 8 byte granularity. The object except Array has two words header. First word has identity hash code and second work has a reference to the object’s class. So, new Object() will be using 8 byte of the heap for the two header words and nothing else because the Object class doesn’t have any fields. After 8 byte of header, the class attributes fallow. Attributes are always aligned in memory to their size. For example int are aligned to a byte granularity, and long are aligned to an 8 byte granularity. The cost to read 4 byte word from memory into a 4 byte register of the processor is much cheaper if the word is aligned to a 4 byte granularity." - very useful info for Java developers. Read it here

P.S. For web applications you can use the following component Session size tag. It lets you calculate memory size for the sessions.

No comments: