Hello, everyone, welcome to Qingge Java, I am Qingge, I heard that self-taught Java people have paid attention to me, you also hurry to join! Welcome ~

Writing in the front

If you already know what Mark Word is, THEN I hope you will read this article, because you may find different entry points to help you understand Mark Word more deeply, it is a good opportunity for you to consolidate your knowledge, but also a technical exchange. A good programmer wouldn’t miss an opportunity like this!

If you don’t know what Mark Word is, then you should read this article carefully, because Mark Word is not only a Word you can use to hold X, it is actually a very important concept, a very important knowledge point, and it is necessary for you to learn the various locks in Java. Why read this article?

Because, qingge’s article, ground gas, easy to understand, that we do it together!


Let’s start with Mark Word

What is Mark Word? Mark Word is an object Word. It is an object Word. Mark Word is an object Word.

class OneClass{
    
}
Copy the code

What is this? A very, very simple class that doesn’t do anything, just declares a class, right? Ok, so here’s what happens:

public static void main(String[] args) {
        System.out.println(ClassLayout.parseInstance(new OneClass()).toPrintable());
}
Copy the code

What is this? Let’s see what the print is.Does that make sense? The red box is the object tag Mark Word, so the purpose of today’s post is to help you understand what this image is. And what’s important!

In advance, today’s article, more dry goods, looks a little laborious, please get ready first!

Open dry!


Let’s start with object instances

** What is an object instance? * * said simple point, our new out of something is an object instance, what is usual said instantiation, is that you created in the heap in the instance of the object, ok, we all know believe in the concept, don’t go, then you might not know, you said for an object instance, it has what parts?

  1. Object head
  2. The instance data
  3. Alignment filling

What’s all this? Here we can under the analogy to us, a object instance is like a complete person, take yourself to the line, here we take you to analogy, the object is your brain, then the instance data is just like body, and align the filling is just like your feet, draw a figure is roughly like this:So maybe this alignment padding is a little confusing here, what does it mean? Let’s say you have a job interview and they say you’re 5 ‘2 “, but you’re 5’ 3 “, so the insoles save you, you know what I mean. The same is true of alignment padding, alignment padding requires that the size of an object instance must be a multiple of 8 bytes, so if you don’t have enough, alignment padding works and gives you a multiple of 8 bytes, you get the idea!

Now we know that an object instance includes the object header, instance data, and alignment padding.What does that mean? There are two parts to an object header: the Pointer (Mark Word) and the Class Pointer (Mark Word).

Instance data and alignment padding

Here is a brief introduction to what is the instance data, to help you have an intuitive understanding!

What is instance data? Write a simple code to illustrate:

We’ve defined a super simple Person class with nothing in it, so we instantiate a Person object that looks like this:

Do you know how big we’re dealing with at this point? In fact, it’s 16 bytes right now, which is the Mark Word and the type pointer in the object header, so to put it simply, when you start creating an empty Java object, you start with just the object header, which is what makes up the object headerThe object Mark Word is 8 bytes, and the type pointer is 8 bytes (with pointer compression it is only 4 bytes, so it aligns and fills 4 bytes), a total of 16 bytes, if we add a little more code at this point, like this:

We give the Person class adds a property, we know that the int account for four bytes, plus 16 bytes object head, this time a total of 20 bytes, but the size of an object if multiples of 8 bytes, the current 20 bytes, so need to 24 bytes to conform to the conditions, at this time will need to align fill four bytes, eventually add up to 24 bytes, Some of the things in the Person class (such as the int defined here) are instance data, and you’ll know what padding is.

Ok, now that we’re here, what is the instance data and what is the alignment padding? Of course, if your current understanding is not very thorough it doesn’t matter, only a rough idea of what is going on is OK!

What is a type pointer

What does that mean? Let’s see, for example, if we create a Person object, what would we normally write, right:

Person p = new Person()
Copy the code

Don’t look at a simple line of code, you score a few concepts clearly, the first is the new person (), the most familiar with, the people is to create a new object, where is created in the heap, and then the p, it is what, this is the object reference, storage where know, this is a memory, the stack, then this person? Is it not too clear to many people?

Remember, this Person can be thought of as a template, just like a human, and then new Person() produces a specific Person, and this Person is a mold that produces that Person, and the most important thing to know is where this Person exists, it’s in the method area, combined with the object tag above, Here’s a picture:

Be sure to note where the Person is located! It is the existence of method area, this is the knowledge of the Java virtual machine, first for a simple understanding!

And the type pointer here is a pointer to this Person class! Do you have a simple idea of what a pointer to a type is? Is no longer a strange word!

This ok, let’s continue, the heavy difficulty is still in the back!


Rounding Mark Word

For a Java object, the object header is extremely important. The object header consists of an object tag and a pointer to a type. The object tag, which is the Mark Word, is important for 64-bit JVMS. It is composed of the following:When I first see this picture, I will feel confused. It’s ok, it’s normal. I just started learning, so do not worry. First of all, remember that this is what a Mark Word looks like inside! As we said before, the object Mark Word is 8 bytes, which is also called 64bit, and what is stored in this 64bit? The graph above gives the answer. You can see here:

A Mark Word is an 8-byte 64-bit Word. What is stored in it depends on the image above. Here are some things you should know:

A Java object consists of the object header, instance data, and alignment padding. The object header is extremely important. The object header is made up of the object tag Mark Word and the type pointer.

Ok, above content is clear! Let’s move on!

Let’s take a look at this Mark Word in detail.

We all know that in the case of concurrency, we need to lock the object to be thread safe. In fact, the locking is the locking of the object. How to determine or know whether the object is locked or what is locked is stored in the Mark Word of the object header. Above the picture is about a Mark Word concrete structure, it can be seen that a Java object actually under different condition, it is not the same, mainly have different locks, such as no lock, lock, lightweight what biased locking, heavyweight lock, etc., different locks, in Mark Word will have different state of tags.

The introduction of JOL

Just look at these, in fact, it is rather obscure, the next need to look at the code:

So this is an empty class, there’s nothing there, so how do you look at this object Mark Word? If your Java project is a Maven project, you can introduce POM dependencies and add the following dependencies:

<dependency>
  <groupId>org.openjdk.jol</groupId>
  <artifactId>jol-core</artifactId>
  <version>0.10</version>
</dependency>
Copy the code

What does this dependency do? This is a code tool called JOL, which is called Java Object Layout. It is used to analyze the size and distribution of Java objects in the Virtual machine.

image.png

So we can see something about using the virtual machine. Look at the printout:Let’s take a look at the empty class we created. We can do this:

MyClass myClass = new MyClass(); / / print out the related object header information System. The out the println (ClassLayout. ParseInstance (myClass). ToPrintable ());Copy the code

The following is obtained:

OK, the information is printed out, remember this is the relevant internal information of myClass, so what is it? If you look at the picture above, does it have an Object header? It’s the head of the object, and here we go:

image.png

What is this? The object header contains the object marker 8 bytes and the type pointer 8 bytes. Let’s look at the object marker first.

Is the starting position is zero, then forward 4 bytes, starting position at this time becomes a 4, then forward 4 bytes, the starting position at this time was 8, the object tag for 8 bytes, it do not have what problem, then we see, the starting position is 8, and move on 4 bytes, arrived at 12, actually type pointer is over at this time, That’s 8 through 12, and the type pointer takes up 4 bytes. Look at this sentence:

At this point, the object is 12 bytes, but isn’t it 16 bytes? We know that the Size of the Java object needs to be an integer multiple of 8 bytes, so in order to satisfy this, we fill in another 4 bytes, finally reaching 16 bytes.

Why is that? This is actually because there is pointer compression, and before we talk about pointer compression, we need to understand this:

Let’s look at this in code:

Let’s add a char to the empty class and look at the result:

image.png

Let’s look at one more:

And I said how many bytes did you say would be aligned? Think about it:

image.png

Why is it 6? Make sure you figure it out. Remember multiples of 8! How big is an object? Remember to see here:

Pointer to the compressed

In this case, we need a JVM argument, which is this:

java -XX:+PrintCommandLineFlags -version
Copy the code

Let’s look at the implementation:

Pointer compression is enabled by default, so our type pointer is now 4 bytes. We can then close the pointer compression by doing this:

Then look at:

That’s right, the object marker is 8 bytes, and the type pointer is 8 bytes, which is the first 16 bytes of the object.

As we’ve said before, a Java object consists of object headers, instance data, and alignment padding. After we’ve explained above, take a look at the following figure, which I’m sure you can see in seconds:

Is there a sense of enlightenment? At this point, we need to analyze the final wave, which is the value value, which is the score value (note that we focus on the value value of Mark Word) :

image.png

The value value analysis

Here’s another very important picture to look at:Then look at the class we created here:

This is a normal class with no locks, which is the most common unlocked class. Let’s see what its object structure is:

image.png

Let’s focus on the value part, and here we still need to look at the structure of Mark Word, which is unlocked, so what we need to look at is here:

In a moment we need to look at the lock bit, and then look at a comparison:

A picture to understand Mark Word

Take a closer look at this picture. If you understand this, you’ll almost certainly have Mark Word! To make it clear, here’s another picture:

conclusion

Mark Word is a very important Word for a Java object, and it’s something you’ll need to learn later when you’re learning concurrent programming and studying locks in Java. It may seem like a new Word at first, but as you get closer to it, it’s a new Word. When you get to know it, you’ll find that it really helps you solve a lot of things that seem difficult to you, and it makes you think that things that seem so high, such as biased locking, lightweight locking, etc. Anyway, as a Java programmer, Mark Word is something you need to master!

Of course, due to the limited personal level, there may be some mistakes in this description, please kindly correct, let’s learn together and make progress together!