Have you ever wondered what the java objects look like from the inside?
Under the cat there will be a detailed description of the java object header, what it consists of and how much memory it takes.
First, remember that in jvm any object in memory consists of the object header and object variables (links and primitives). Also, the final object size can be expanded so that the size becomes a multiple of 8 bytes.
The title of each object (except for the array) consists of two machine words - mark word and class word . Arrays have an additional 32 bits to describe the length of the array.
Mark word stores identity hashcode, bits used by the garbage collector and bits used for locks. More details can always be found in the corresponding OpenJDK markOop.hpp sorts .
In Class word, a pointer is stored to the class itself, that is, to the place where the information about this data type lies: methods, annotations, inheritance, and more. More details can also always be found in the corresponding OpenJDK klass.hpp sorts .
Let's now take a closer look at the title of the object, and in particular the mark word.
As can be seen from the table, the content of Mark word can vary greatly depending on the current state of the object.
When Biased Locking is turned on, the object shifts to the first object that captures its monitor. Subsequent capture of the object by the same stream will be slightly faster.
Here are the main theoretical prerequisites for this lock:
- Throughout the life of the object, it is mostly owned by one thread.
- If the thread recently used a lock on this object, most likely the processor's cache will still contain data that will be needed to re-capture this object.
Biased Locking is enabled by default starting with java 6, -XX: -UseBiasedLocking
That is, in the table, the state of an object is determined by the combination of the biased_lock and lock bits.
In this mode, it is assumed that the time taken by this object by different threads does not intersect at all or intersects slightly. In this mode, instead of heavy locks on the operating system, the JVM uses atomics.
So, in a 32-bit jvm object header consists of 8 bytes. Arrays are additionally 4 bytes.
On a 64-bit jvm, the object header consists of 16 bytes. Arrays are additionally 4 bytes.
An object header consists of 12 bytes. Arrays are additionally 4 bytes.
A little bit about pointer compression. For a 32-bit pointer, the address space is limited to 4GB. However, if we recall again that in jvm the size of an object is a multiple of 8 bytes, then we can use a pseudo 35 bit pointer, with three zeros at the end. And, thus, to refer to 32GB of memory. Compression turns out not free, the price - additional operation (pointer << 3) at any appeal to heap.
I would also like to add that everything described here is not a dogma, perhaps in other versions of jvm the title of the object will be different. Described here is relevant for openjdk 8.
Source: https://habr.com/ru/post/447848/
All Articles