Java 对象头
以 32 位虚拟机为例
普通对象
1. |--------------------------------------------------------------| 2. | Object Header (64 bits) | 3. |------------------------------------|-------------------------| 4. | Mark Word (32 bits) | Klass Word (32 bits) | 5. |------------------------------------|-------------------------|
1. |---------------------------------------------------------------------------------| 2. | Object Header (96 bits) | 3. |--------------------------------|-----------------------|------------------------| 4. | Mark Word(32bits) | Klass Word(32bits) | array length(32bits) | 5. |--------------------------------|-----------------------|------------------------|
其中 Mark Word 结构为
1. |-------------------------------------------------------|--------------------| 2. | Mark Word (32 bits) | State | 3. |-------------------------------------------------------|--------------------| 4. | hashcode:25 | age:4 | biased_lock:0 | 01 | Normal | 5. |-------------------------------------------------------|--------------------| 6. |thread:23|epoch:2| age:4 | biased_lock:1 | 01 | Biased | 7. |-------------------------------------------------------|--------------------| 8. | ptr_to_lock_record:30 | 00 | Lightweight Locked | 9. |-------------------------------------------------------|--------------------| 10. | ptr_to_heavyweight_monitor:30 | 10 | Heavyweight Locked | 11. |-------------------------------------------------------|--------------------| 12. | | 11 | Marked for GC | 13. |-------------------------------------------------------|--------------------|
64 位虚拟机 Mark Word
1. |--------------------------------------------------------------------|--------------------| 2. | Mark Word (64 bits) | State | 3. |--------------------------------------------------------------------|--------------------| 4. | unused:25 | hashcode:31 | unused:1 | age:4 | biased_lock:0 | 01 | Normal | 5. |--------------------------------------------------------------------|--------------------| 6. | thread:54 | epoch:2 | unused:1 | age:4 | biased_lock:1 | 01 | Biased | 7. |--------------------------------------------------------------------|--------------------| 8. | ptr_to_lock_record:62 | 00 | Lightweight Locked | 9. |--------------------------------------------------------------------|--------------------| 10. | ptr_to_heavyweight_monitor:62 | 10 | Heavyweight Locked | 11. |--------------------------------------------------------------------|--------------------| 12. | | 11 | Marked for GC | 13. |--------------------------------------------------------------------|--------------------|