Krab
Krab

Reputation: 6756

Size of Java object

How is it possible that minimal size of Java object is 8 bytes (only the object header),

What is the memory consumption of an object in Java?

if in the C++ class representing the java object,

http://hg.openjdk.java.net/jdk7/jdk7/hotspot/file/9b0ca45cd756/src/share/vm/oops/oop.hpp

i can see that the class has more members

class oopDesc {
  friend class VMStructs;
 private:
  volatile markOop  _mark; // this is the object header
  union _metadata {
    wideKlassOop    _klass;
    narrowOop       _compressed_klass;
  } _metadata; // what about size of this member?

Upvotes: 1

Views: 259

Answers (2)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44042

Because the header data is binary encoded. When accessing this information via JVMTI or a native call, this binary data is exploded into data types with a wider ranger.

Furthermore, these sizes are an implementation detail and vary dependant on the bitness of the VM and on the fact of the VM using so-called compressed oops. You can read out the actual header of an instance using the JOL tool that is distributed in the OpenJDK. Also, you can find documentation of encoding of the header in the sourc code.

Upvotes: 0

Jakub Kubrynski
Jakub Kubrynski

Reputation: 14149

It's possible because in 32bit JVM object contains 4 bytes of mark header and 4 bytes of class reference. Mark headers contains different information depending the object type (sizes in bits):

normal objects -> unused:25 hash:31 cms_free:1 age:4 biased_lock:1 lock:2

biased objects -> JavaThread*:54 epoch:2 cms_free:1 age:4 biased_lock:1 lock:2

Upvotes: 1

Related Questions