java對象頭

1)對象頭:包括標記字段和類型指針兩部份內容:數組

1)Mark Word(標記字段):用於存儲運行時對象自身的數據。
	1>佔用內存大小與虛擬機位長一致,在運行期間,考慮到JVM的空間效率,Mark Word被設計成爲一個非固定的數據結構,以便存儲更多有效的數據。
	2>存儲運行時對象自身的數據:
	
		哈希碼(hash)
		GC分代年齡(age)
		鎖標識位(lock):
			00	輕量級鎖
			01	偏向鎖
			10	重量級鎖
		偏向鎖標識位(biased_lock)
			0	無鎖
			1	偏向鎖
		偏向線程ID(JavaThread*)
		偏向時間戳(epoch)
	
	3>Mark Word裏存儲的數據會隨着鎖標誌位的變化而變化,即不一樣的鎖狀態,存儲着不一樣的數據:
	
		鎖狀態	    存儲內容				 鎖標識位            偏向鎖標識位(是不是偏向鎖)
		-------- ------------------------------            --------            --------
		無鎖狀態	    哈希碼、GC分代年齡				01			0
		偏向鎖	    線程ID、偏向時間戳、GC分代年齡		        01			1
		輕量級鎖	    指向棧中鎖記錄的指針				00			無
		重量級鎖	    指向monitor的指針				10			無
		GC標記		無					11			無

2)Class Metadata Address(類型指針):指向對象的類元數據(方法區的Class數據),虛擬機經過這個指針肯定該對象是哪一個類的實例。

2)實例數據:數據結構

存放類的屬性數據信息,包括父類的屬性信息,若是是數組,則還包括了數組的長度。

3)對齊填充:app

因爲虛擬機要求對象起始地址必須是8字節的整數倍,填充數據不是必須存在的,僅僅是爲了字節對齊。

4)hotspot/src/share/vm/oops/markOop.hpp 源碼中的說明:dom

// The markOop describes the header of an object.
//
// Note that the mark is not a real oop but just a word.
// It is placed in the oop hierarchy for historical reasons.
//
// Bit-format of an object header (most significant first, big endian layout below):
//
//  32 bits:
//  --------
//             hash:25 ------------>| age:4    biased_lock:1 lock:2 (normal object)
//             JavaThread*:23 epoch:2 age:4    biased_lock:1 lock:2 (biased object)
//             size:32 ------------------------------------------>| (CMS free block)
//             PromotedObject*:29 ---------->| promo_bits:3 ----->| (CMS promoted object)
//
//  64 bits:
//  --------
//  unused:25 hash:31 -->| unused:1   age:4    biased_lock:1 lock:2 (normal object)
//  JavaThread*:54 epoch:2 unused:1   age:4    biased_lock:1 lock:2 (biased object)
//  PromotedObject*:61 --------------------->| promo_bits:3 ----->| (CMS promoted object)
//  size:64 ----------------------------------------------------->| (CMS free block)
//
//  unused:25 hash:31 -->| cms_free:1 age:4    biased_lock:1 lock:2 (COOPs && normal object)
//  JavaThread*:54 epoch:2 cms_free:1 age:4    biased_lock:1 lock:2 (COOPs && biased object)
//  narrowOop:32 unused:24 cms_free:1 unused:4 promo_bits:3 ----->| (COOPs && CMS promoted object)
//  unused:21 size:35 -->| cms_free:1 unused:7 ------------------>| (COOPs && CMS free block)
//
//  - hash contains the identity hash value: largest value is
//    31 bits, see os::random().  Also, 64-bit vm's require
//    a hash value no bigger than 32 bits because they will not
//    properly generate a mask larger than that: see library_call.cpp
//    and c1_CodePatterns_sparc.cpp.
//
//  - the biased lock pattern is used to bias a lock toward a given
//    thread. When this pattern is set in the low three bits, the lock
//    is either biased toward a given thread or "anonymously" biased,
//    indicating that it is possible for it to be biased. When the
//    lock is biased toward a given thread, locking and unlocking can
//    be performed by that thread without using atomic operations.
//    When a lock's bias is revoked, it reverts back to the normal
//    locking scheme described below.
//
//    Note that we are overloading the meaning of the "unlocked" state
//    of the header. Because we steal a bit from the age we can
//    guarantee that the bias pattern will never be seen for a truly
//    unlocked object.
//
//    Note also that the biased state contains the age bits normally
//    contained in the object header. Large increases in scavenge
//    times were seen when these bits were absent and an arbitrary age
//    assigned to all biased objects, because they tended to consume a
//    significant fraction of the eden semispaces and were not
//    promoted promptly, causing an increase in the amount of copying
//    performed. The runtime system aligns all JavaThread* pointers to
//    a very large value (currently 128 bytes (32bVM) or 256 bytes (64bVM))
//    to make room for the age bits & the epoch bits (used in support of
//    biased locking), and for the CMS "freeness" bit in the 64bVM (+COOPs).
//
//    [JavaThread* | epoch | age | 1 | 01]       lock is biased toward given thread
//    [0           | epoch | age | 1 | 01]       lock is anonymously biased
//
//  - the two lock bits are used to describe three states: locked/unlocked and monitor.
//
//    [ptr             | 00]  locked             ptr points to real header on stack
//    [header      | 0 | 01]  unlocked           regular object header
//    [ptr             | 10]  monitor            inflated lock (header is wapped out)
//    [ptr             | 11]  marked             used by markSweep to mark an object not valid at any other time
//
//    We assume that stack/thread pointers have the lowest two bits cleared.
相關文章
相關標籤/搜索