Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is in Java object header?

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.

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?

like image 921
alobodzk Avatar asked Oct 14 '14 09:10

alobodzk


People also ask

What is in memory object in Java?

A stack and a heap are used for memory allocation in Java. However, the stack is used for primitive data types, temporary variables, object addresses etc. The heap is used for storing objects in memory.

What is object in memory?

Object memory is a type of working memory that is not motivated by food rewards or aversive environments but by the natural tendency of mammals to investigate novel objects or familiar objects in novel locations (Ennaceur et al., 1997).

What is memory layout in Java?

A memory layout can be used to describe the contents of a memory segment in a language neutral fashion.

Why objects are stored in heap in Java?

Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory. These objects have global access and we can access them from anywhere in the application.


2 Answers

For HotSpot:

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

The mark word has word size (4 byte on 32 bit architectures, 8 byte on 64 bit architectures) and

the klass pointer has word size on 32 bit architectures. On 64 bit architectures the klass pointer either has word size, but can also have 4 byte if the heap addresses can be encoded in these 4 bytes.

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

You can also find a wiki entry about this 1.

The mark word is actually used for many things.

  1. One is Biased Locking 2 through 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.hashCode one).

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

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

  • 1 https://wiki.openjdk.java.net/display/HotSpot/CompressedOops
  • 2 https://wiki.openjdk.java.net/display/HotSpot/Synchronization
like image 167
box Avatar answered Oct 06 '22 07:10

box


You can find the object layout from HotSpot sources.

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

like image 45
apangin Avatar answered Oct 06 '22 08:10

apangin