java java对象头中有什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26357186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:47:21  来源:igfitidea点击:

What is in java object header

javajvmjava-memory-modelobject-layoutjol

提问by alobodzk

Could you give me some information on what is exactly stored in object header? I know, that it's probably JVM dependent, but maybe for HotSpot at least? I'm looking for exact description specifically for a first row.

你能给我一些关于对象头中究竟存储什么的信息吗?我知道,它可能依赖于 JVM,但至少对于 HotSpot 来说可能?我正在寻找专门针对第一行的确切描述。

I've read several information that I can't verify positively with information I find. Maybe you have a link to OpenJDK wiki that says it all?

我已经阅读了一些无法用我找到的信息进行正面验证的信息。也许你有一个指向 OpenJDK wiki 的链接,它说明了一切?

回答by box

For HotSpot:

对于热点:

The object header consists of a mark word and a klass pointer.

对象头由一个标记字和一个类指针组成。

The mark wordhas word size (4 byteon 32 bit architectures, 8 byteon 64 bit architectures) and

所述标记字具有(字大小4 byte在32位体系结构中,8 byte在64位体系结构)和

the klass pointerhas word size on 32 bitarchitectures. On 64 bitarchitectures the klass pointer either has word size, but can also have 4 byteif the heap addresses can be encoded in these 4 bytes.

克拉斯指针对字大小32 bit的架构。在64 bit体系结构上,klass 指针要么具有字大小,但4 byte如果堆地址可以在这些4 bytes.

This optimization is called "compressed oops"and you can also control it with the option UseCompressedOops.

这种优化称为“压缩 oops”,您也可以使用选项来控制它UseCompressedOops

You can also find a wiki entry about this 1.

您还可以找到有关此1的 wiki 条目。

The mark wordis actually used for many things.

标志词实际上是使用了很多东西。

  1. One is Biased Locking2through which HotSpot can implement efficient locking.
  2. It is also used during GC to set forward pointers, and to store the age of the objects. The identity hash code of an object can be stored inside the mark (the System.identityHashCode/Object.hashCodeone).
  1. 一个是Biased Locking2,HotSpot可以通过它实现高效的锁定。
  2. 它也用于GC to set forward pointers, 和to store the age of the objects。对象的身份哈希码可以存储在标记(System.identityHashCode/Object.hashCode一)内。

There is a comment in the source code of markOop.hppthat describes the layout depending on the architecture:

markOop.hpp的源码中有一段注释,根据架构描述了布局:

//  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)

You can also find the oop header file here.

您还可以在此处找到 oop 头文件。

回答by apangin

You can find the object layout from HotSpot sources.

您可以从HotSpot 源中找到对象布局。

The header consists of markOopfollowed by a pointer (or compressed pointer) to instanceKlass.

标头由markOop后跟一个指向instanceKlass的指针(或压缩指针)组成。

回答by Chris Bailey

The following presentation gives you a general idea of the object contents and the object header: http://www.slideshare.net/cnbailey/memory-efficient-java

下面的介绍让你大致了解对象内容和对象头:http: //www.slideshare.net/cnbailey/memory-efficient-java

The actual header for any object is JVM vendor, version and object type specific.

任何对象的实际标头都是特定于 JVM 供应商、版本和对象类型的。