Size of Java object

試著忘記壹切 提交于 2020-01-05 15:06:21

问题


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?

回答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




回答2:


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.



来源:https://stackoverflow.com/questions/29143399/size-of-java-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!