1

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?
Community
  • 1
  • 1
Krab
  • 6,526
  • 6
  • 41
  • 78
  • 1
    Why do you think C++ represention has more members? There are just two: _mark and _metadata, each of them is 4 bytes on 32-bit architecture. _metadata is a union, that is, _klass and _compressed_klass share the same space. – apangin Mar 19 '15 at 11:37
  • apangin: yes you are right, my fault, the one member i deleted was static member, markOop has size of 4 bytes – Krab Mar 19 '15 at 11:39

2 Answers2

1

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

Jakub Kubrynski
  • 13,724
  • 6
  • 60
  • 85
0

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.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192