A little interview

Preface honey language

Recently, master horse fire do not do not, although did not grab the mouse tail juice trademark registration right, but must rub a wave of master horse’s heat, the following is the lightning five consecutive whip teaching links, are you ready?

Before the formal content begins to dump two articles about class loading mechanism and memory layout, because today’s content is more or less directly related to these two articles, to this aspect is relatively weak friends can first see, I put the address below.

JVM internal competition Java memory area, job-hopping big factory will know knowledge!

Moon doesn’t talk about martial arts!! A class loading mechanism to the interviewer said confused!!

Today’s article is about two questions in a mock interview

1. Object creation process

2. The memory layout of the object

Object creation

The following is based on the HotSpot VM generational model

This is actually a complete illustration of what happens during the creation of an object. Many friends may not understand it at first, so let’s follow the step by step in the upper left corner:

  1. A new object comes out to determine whether the thread stack can be allocated
    • If you can allocate it, allocate it on the stack.
    • If not, proceed to the second step.
  2. Determine if the object is large enough
    • If it’s big enough, it goes straight to the old age.
    • If not, proceed to step 3.
  3. Determine if the thread creating the object has enough TLAB (local thread buffer) space
    • If sufficient, allocate directly in TLAB.
    • If not, enter another space in the Eden zone. Then proceed to step four.
  4. The GC to clear
    • If the object is cleared, it ends directly.
    • If the object is not cleared, proceed to Step 5.
  5. At this point, the object enters Survivor 1 zone and determines whether it is old enough
    • If they are old enough, they go directly into the old area.
    • If not, enter Survivor zone 2, then enter Step 4, and so on.

This flowchart and step-by-step analysis should give you a clear idea of how an object is created, but there are many small details that are ignored. Why does the JVM make such a big deal about object creation? In order to understand it more deeply, let’s look at the following questions:

  1. Why do objects choose to be allocated on the stack first?

First of all, the stack is thread private, and objects are allocated in the stack first. All the information and space of the object can be directly cleared through POP. When the thread dies, this TLAB area can also be directly cleared.

  1. Why does the JVM let large objects go straight into the old age?

Large objects require continuous space to store, which can be a burden to the JVM if you don’t store older objects. If you don’t have enough space, a GC may be triggered early to clean up space to house large objects.

  1. Why did you choose TLAB first?

TLAB is a thread-local buffer. The advantage of TLAB is to prevent competition between objects created by different threads from selecting the same memory region, which greatly reduces the probability of competition.

  1. Why are there two Survivor zones? And the ones that are alive and not old enough are going to go from Survivor to Survivor, right?

According to the root reachable algorithm, the JVM will find all the objects that are in use from the beginning, and the ones that are not in use are garbage. In general, many objects are discarded when they are used up, so there are very few objects that really survive for a long time. After moving these objects from Survivor to Survivor, You can directly recycle the full amount of space in the Survivor area, which is very efficient.

Object memory layout

Object o = new Object(); Object o = new Object(); How many bytes does it take? The purpose of this question is to see if you have a clear understanding of the memory layout of the object.

The memory layout of objects in Java is divided into two cases, non-array objects and array objects, and the difference between array objects and non-array objects is that they need extra space to store the length of the array.

Object head

The object header is divided into MarkWord and Class Pointer.

  • MarkWord: contains a series of tag bits, such as lightweight lock bits, bias lock bits, GC record information, etc., which account for 4 bytes on 32-bit systems and 8 bytes on 64-bit systems.

  • ClassPointer: Used to point to the memory address of the corresponding Class object (and its corresponding metadata object) of the object. It is 4 bytes on 32-bit systems and 8 bytes on 64-bit systems.

  • Length: exists only in array objects and is used to record the Length of an array. It takes 4 bytes

Interface data
  • Interface data: the actual data of an object. The actual data of an object includes all the member variables of the object. The size of the object is determined by the size of each member variable. (Static member variables are not included here, as they are maintained in the method area.)
Padding
  • Padding:Java objects are 8 bytes aligned, that is, all Java objects have to take a multiple of 8, because when we take a piece of data from disk, we don’t say I want to take a byte, it’s a byte, we take it all together, it’s a block of 8 bytes, so to be complete, The padding just adds bytes, making sure the object is an integer multiple of 8 bytes.

Moon specifically noted the difference in the size of the different regions on 32-bit and 64-bit JVMS. This is because object Pointers are longer to address on 64-bit JVMS and therefore take up more space than on 32-bit JVMS.

But now imagine a scenario, the company now project deployment machine is 32 bit, does your boss let you will project moving to 64 – bit system, but also because a 64 – bit system is more take up more space than a 32-bit system, how to do, because normally we don’t need this part of the extra space, so the JVM has help you consider good, That’s pointer compression.

Pointer to the compressed

The -xx :+UseCompressedOops parameter is the solution that the JVM gives you. It compresses the pointer and reduces the size of the classpointer parameter by half to save space.

Object O = New Object()

After learning the memory layout just now, this question is easy to answer. The interviewer actually wants to ask you what is the memory layout of the object. Here, we will analyze the results of this question.

  • With pointer compression enabled, MarkWord takes 4 bytes, Classpoint takes 8 bytes, and Interface Data takes no data, for a total of 12 bytes. Since the object needs to be an integer multiple of 8, the Padding adds 4 bytes, for a total of 16 bytes of storage.

  • In the absence of Pointers, MarkWord takes 8 bytes, Classpoint takes 8 bytes, and Interface Data takes no data, for a total of 16 bytes.

conclusion

Today’s article tells you about the creation process of an object, from its birth to its death, what happens? Also illustrates in detail all the object’s memory layout, deeply dissected the body structure of an object, the interviewer ask you again, but is in for a chat, the content of this article is the liver, need everyone read once, of course, also can pay attention to my public, ‘where did you get the moon’, privately contact me, I am the moon, see you next time ~