0

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?

1

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.