首页 > 编程知识 正文

java获取对象在内存的大小,java对象的组成部分

时间:2023-05-05 04:39:02 阅读:129878 作者:2367

在两种说法中,目标头涵盖的内容不同,前者指向的Object Header只是傲慢的路灯Oop指针大小——,在32位计算机上指针大小32位(4字节),在64位计算机上指针大小64 后者除了傲慢的路灯Oop指针外,还包含一个名为Klass*的类型元数据,用于描述Metaspace中Heap对象的实际类型。 如果在不启用压缩指针: -XX: UseCompressedOops的情况下启用objecccecops压缩指针,则此Klass*指针的大小为64位虚拟机的Klass*指针为naroros 因为是union类型,_metadata占用64位内存空间,多余的32位空间会空出——。我倾向于将普通对象的头部定义为【冷路灯Oop Klass*】的组合,所以后者的结论更合适

基于此,在HotSpot VM中,常规对象的对象标头定义如下:

volatile冷路灯Oop _冷路灯;

union _metadata {

Klass* _klass;

narrowKlass _compressed_klass;

} _metadata;

对于特殊对象(如数组),其对象标题包含用于描述数组的长度。 在HotSpot VM上启用压缩指针实际上会占用_metadata中空闲的后面4字节内存空间。 在这种情况下,128位内容区域可以表示数组的对象标头。 指针压缩-如果未启用-XX:-UseCompressedOops? 添加到嵌入的实例数据的末尾。 此时,考虑到字节对齐,无法指定正确的大小。

//The layout of array Oops is:

//

//傲慢的路灯Oop

//klass *//32 bitsifcompressedbutdeclared 64 inlp 64。

//length//sharesklassmemoryorallocatedafterdeclaredfields。

傲慢的路灯Oop用于存储运行时中对象的有用信息,如哈希代码(HashCode )、GC世代年龄、锁定状态标志、线程具有的锁定、线程ID、时间戳和传输指针虚拟机使用这些紧凑的bitMap描述对象同步过程和GC过程所需的信息,帮助虚拟机运行时实现对象同步机制和垃圾回收的功能——即使看到r很大,也可能会吐槽

返回提问者,有关对象标题中是否存储有类型的地址信息——的回答,请参照以上内容。 这就是所谓的Klass*。 有关JavaThread*线程所有者的指针大小的信息,请参阅以下注释—— : 在HotSpot VM上,Thread在新建时就已经采用了一定的算法,控制指针大小,例如32位VM只占用23位内存空间——,请放心放入。

//The傲慢的路灯oopdescribestheheaderofanobject。

//

//Note that the傲慢的路灯is not a real oop but just a word。

//itisplacedintheoophierarchyforhistoricalreasons。

//

//bit-formatofanobjectheader (mostsignificantfirst,big endian layout below ) :

//

//32 bits:

//----

//hash :25----|age :4 biased _ lock 33601 lock :2 (正规对象) )。

//Java thread * :23 epoch :2 age :4 biased _ lock :1 lock :2 (biased object ) )。

//size 336032----------------------|cmsfreeblock )

//promoted object * :29----|promo _ bits 33603----|(cmspromotedobject )

//

//64 bits:

//----

//unused :25 hash :31---| unused :1 age :4 biased _ lock :1 lock :2 (正规对象) )。

//Java thread * :54 epoch :2 unused :1 age :4 b

iased_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] 冷傲的路灯ed used by 冷傲的路灯Sweep to 冷傲的路灯 an object

// not valid at any other time

//

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。