打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java对象头
  1. HotSpot虚拟机中,对象在内存中的布局分为三块区域:对象头、实例数据和对齐填充。  
  2.   
  3. 1)对象头:包括标记字段和类型指针两部分内容(注:如果是数组对象,则包含三部分内容):  
  4.   
  5.     1)Mark Word(标记字段):用于存储运行时对象自身的数据。  
  6.         1>占用内存大小与虚拟机位长一致,在运行期间,考虑到JVM的空间效率,Mark Word被设计成为一个非固定的数据结构,以便存储更多有效的数据。  
  7.         2>存储运行时对象自身的数据:  
  8.           
  9.             哈希码(hash)  
  10.             GC分代年龄(age)  
  11.             锁标识位(lock):  
  12.                 01  无锁  
  13.                 01  偏向锁  
  14.                 00  轻量级锁  
  15.                 10  重量级锁  
  16.             偏向锁标识位(biased_lock)  
  17.                 0   无锁  
  18.                 1   偏向锁  
  19.             偏向线程ID(JavaThread*)  
  20.             偏向时间戳(epoch)  
  21.           
  22.         3>Mark Word里存储的数据会随着锁标志位的变化而变化,即不同的锁状态,存储着不同的数据:  
  23.           
  24.             锁状态     存储内容                            锁标识位        偏向锁标识位(是否是偏向锁)        
  25.             --------    ------------------------------      --------        --------  
  26.             无锁状态    哈希码、GC分代年龄                  01              0  
  27.             偏向锁     线程ID、偏向时间戳、GC分代年龄       01              1  
  28.             轻量级锁    指向栈中锁记录的指针                  00              无  
  29.             重量级锁    指向monitor的指针                    10              无  
  30.             GC标记        无                               11              无  
  31.       
  32.     2)Class Metadata Address(类型指针):指向对象的类元数据(方法区的Class数据),虚拟机通过这个指针确定该对象是哪个类的实例。  
  33.       
  34.     3)如果对象是数组类型,则对象头中还存储着数组的长度。  
  35.       
  36.       
  37.           
  38. 2)实例数据:存放类的属性数据信息,包括父类的属性信息,如果是数组,则还包括了数组的长度。  
  39.   
  40.   
  41. 3)对齐填充:由于虚拟机要求对象起始地址必须是8字节的整数倍,填充数据不是必须存在的,仅仅是为了字节对齐。  
  42.       
  43.       
  44. 4)hotspot/src/share/vm/oops/markOop.hpp 源码中的说明:  
  45.   
  46.     // The markOop describes the header of an object.  
  47.     //  
  48.     // Note that the mark is not a real oop but just a word.  
  49.     // It is placed in the oop hierarchy for historical reasons.  
  50.     //  
  51.     // Bit-format of an object header (most significant first, big endian layout below):  
  52.     //  
  53.     //  32 bits:  
  54.     //  --------  
  55.     //             hash:25 ------------>| age:4    biased_lock:1 lock:2 (normal object)  
  56.     //             JavaThread*:23 epoch:2 age:4    biased_lock:1 lock:2 (biased object)  
  57.     //             size:32 ------------------------------------------>| (CMS free block)  
  58.     //             PromotedObject*:29 ---------->| promo_bits:3 ----->| (CMS promoted object)  
  59.     //  
  60.     //  64 bits:  
  61.     //  --------  
  62.     //  unused:25 hash:31 -->| unused:1   age:4    biased_lock:1 lock:2 (normal object)  
  63.     //  JavaThread*:54 epoch:2 unused:1   age:4    biased_lock:1 lock:2 (biased object)  
  64.     //  PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object)  
  65.     //  size:64 ----------------------------------------------------->| (CMS free block)  
  66.     //  
  67.     //  unused:25 hash:31 -->| cms_free:1 age:4    biased_lock:1 lock:2 (COOPs && normal object)  
  68.     //  JavaThread*:54 epoch:2 cms_free:1 age:4    biased_lock:1 lock:2 (COOPs && biased object)  
  69.     //  narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 ----->| (COOPs && CMS promoted object)  
  70.     //  unused:21 size:35 -->| cms_free:1 unused:7 ------------------>| (COOPs && CMS free block)  
  71.     //  
  72.     //  - hash contains the identity hash value: largest value is  
  73.     //    31 bits, see os::random().  Also, 64-bit vm's require  
  74.     //    a hash value no bigger than 32 bits because they will not  
  75.     //    properly generate a mask larger than that: see library_call.cpp  
  76.     //    and c1_CodePatterns_sparc.cpp.  
  77.     //  
  78.     //  - the biased lock pattern is used to bias a lock toward a given  
  79.     //    thread. When this pattern is set in the low three bits, the lock  
  80.     //    is either biased toward a given thread or "anonymously" biased,  
  81.     //    indicating that it is possible for it to be biased. When the  
  82.     //    lock is biased toward a given thread, locking and unlocking can  
  83.     //    be performed by that thread without using atomic operations.  
  84.     //    When a lock's bias is revoked, it reverts back to the normal  
  85.     //    locking scheme described below.  
  86.     //  
  87.     //    Note that we are overloading the meaning of the "unlocked" state  
  88.     //    of the header. Because we steal a bit from the age we can  
  89.     //    guarantee that the bias pattern will never be seen for a truly  
  90.     //    unlocked object.  
  91.     //  
  92.     //    Note also that the biased state contains the age bits normally  
  93.     //    contained in the object header. Large increases in scavenge  
  94.     //    times were seen when these bits were absent and an arbitrary age  
  95.     //    assigned to all biased objects, because they tended to consume a  
  96.     //    significant fraction of the eden semispaces and were not  
  97.     //    promoted promptly, causing an increase in the amount of copying  
  98.     //    performed. The runtime system aligns all JavaThread* pointers to  
  99.     //    a very large value (currently 128 bytes (32bVM) or 256 bytes (64bVM))  
  100.     //    to make room for the age bits & the epoch bits (used in support of  
  101.     //    biased locking), and for the CMS "freeness" bit in the 64bVM (+COOPs).  
  102.     //  
  103.     //    [JavaThread* | epoch | age | 1 | 01]       lock is biased toward given thread  
  104.     //    [0           | epoch | age | 1 | 01]       lock is anonymously biased  
  105.     //  
  106.     //  - the two lock bits are used to describe three states: locked/unlocked and monitor.  
  107.     //  
  108.     //    [ptr             | 00]  locked             ptr points to real header on stack  
  109.     //    [header      | 0 | 01]  unlocked           regular object header  
  110.     //    [ptr             | 10]  monitor            inflated lock (header is wapped out)  
  111.     //    [ptr             | 11]  marked             used by markSweep to mark an object not valid at any other time  
  112.     //  
  113.     //    We assume that stack/thread pointers have the lowest two bits cleared.  
  114.   
  115.       

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
关于 锁的四种状态与锁升级过程 图文详解
HotSpot虚拟机对象探秘
java开发技术之synchronized的使用浅析
轻量级锁与偏向锁
线程
在C#中多用户或多线程对用同一个XML进行同时操作,应该如何避免
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服