Please help with the following question. Assume that I have class that contains only methods. Will space in heap be allocated for objects created of this class? If yes then what does it contain?
-
Please see the answers here - https://stackoverflow.com/questions/258120/what-is-the-memory-consumption-of-an-object-in-java – Fairoz Jun 05 '17 at 15:09
1 Answers
The question linked by Fairoz contains most relevant data, but I'll try to narrow information to your case.
Yes. The JVM will take a contiguous space off the heap to store these objects.
The contents are specific to the JVM implementation. In HotSpot, you can see the specifics in the source code.
- There will be a machine word called "Mark", which is defined here, and is used to keep the hashCode, locking state, and garbage collection. This takes 8 bytes.
- Next will be a pointer to the Klass, which contains information about the class, such as methods.
If you're in a 64 bit JVM, with compressedOops enabled (as is default on java 8) the Klass pointer will take only 4 bytes. Since you have no fields, the total size is 12 bytes. However, the JVM forces to align to a full word, so your object will use 4 bytes for padding. In total, 16 bytes.
Some useful documentation: - https://www.infoq.com/articles/Introduction-to-HotSpot - https://psy-lob-saw.blogspot.com.es/2013/05/know-thy-java-object-memory-layout.html

- 440
- 2
- 9