“This is the 14th day of my participation in the First Challenge 2022.


  • j3_liuliang
  • Always teasing no object on their own new object bai! I used to do this a lot, but new objects for us to use (don’t get the wrong idea) are much easier than us, but have you ever thought about how objects you use are created? In this article we’ll talk about how an Object is created by a virtual machine and used by us;

At last we saw the Java virtual machine run time data area (haven’t seen you can click here) to know where to put what types of data, that believe that friends should know we often use the object are almost to create and allocate memory in the heap (I used the word “almost”, regardless of the escape analysis and scalar replacement), don’t be fooled! I’m sure you don’t know the details (smug look), such as how the virtual machine knows to create an object, how to create that object, how to allocate object memory, etc.

That, you patiently listen to me continue to ramble ramble!

Object creation

1.1 Illustrated object creation process

Take a look at this diagram, and the following five parts will be introduced in detail

1.2 Class loading Check

As mentioned above, how does the virtual machine know when the program is creating an object?

When the virtual machine hits the new command, it knows that the guy wants me to create an object for him. That it is not completely listen to this guy to look (to verify), it is first to test whether this young man is that I do card members (parameters will be able to check the instructions in the constant pool positioning to this class of symbol reference, and check whether the notation to refer to the class of the representative has been loaded, parsed and initialization) if you don’t know, Sorry to open a card and then come to me to create an object;

1.3 Allocating Memory

Now that the lad has a card with me (class load check passed), let’s start the next process! Allocate memory

The task of allocating spatial memory is essentially the same as dividing a certain size block of memory from the Java heap.

Young man I want to explain a point, I here but formal creation object center, you here to handle the card only 88 yuan, then I can only give you the corresponding service — allocation of memory size can be determined when loading (handling cards) (88 yuan).

There are two ways to allocate memory in a virtual machine:

  1. Pointer collision:

    If memory in the Java heap is perfectly neat, all used memory is placed on one side, free memory is placed on the other, and a pointer is placed in the middle as an indicator of the dividing point. The allocated memory simply moves that pointer to free space by an equal distance to the size of the object. This allocation is called a “Bump The Pointer.”

  2. Free list:

    If the memory in the Java heap is not neat, has been used memory and free memory staggered together, that is simply no way pointer collision, the virtual machine, you must maintain a list of records on which memory blocks are available, at the time of distribution from the list to find a large enough space division to the object instance, and update the list of records, This type of allocation is called a Free List.

This is a problem, exactly what kind of service to provide this boy!

The choice of allocation method is determined by the cleanliness of the Java heap, which in turn is determined by the ability of the garbage collector to Compact.

Mention garbage collection algorithms (four) that will be covered in more detail in future articles, but for now just know:

  1. Replication algorithm
  2. Marked – clear
  3. Mark-compress
  4. Generational collection

When using garbage collection algorithm with compression process (Serial, ParNew garbage collector), the system uses the allocation algorithm is pointer collision, which is simple and efficient; The CMS garbage collector, on the other hand, has to use a more complex free list to allocate memory.

1.3.1 Concurrency Problems during Memory allocation

Okay, so now I’m going to give this guy some way to create objects, but the question is, right?

If when creating an object for this young man, someone inserts a horizontal knife in the middle to seize love, that how to do!

The virtual machine object creation process is not an atomic operation, but a multi-step operation which is not safe when concurrent

Anyway I (virtual machine) also stem this line of, have no point ability then return how mix;

Virtual machines use two methods to ensure thread safety:

  1. CAS+ Retry on failure: CAS is an implementation of optimistic locking. Optimistic locking is when an operation is performed without locking but with no conflicts, and if it fails because of conflicts, it is retried until it succeeds. The CENTRAL Authentication Service (CAS) configuration fails to retry to ensure atomicity of update operations.

  2. TLAB: each thread is allocated a block of memory in Eden area in advance. When THE JVM allocates memory to objects in the thread, it first allocates memory in TLAB. When the object is larger than the remaining memory in TLAB or the memory in TLAB is exhausted, the CAS is used for memory allocation.

    About TLAB

    TLAB stands for Thread Local Allocation Buffer, which is a thread-specific memory Allocation area. A private object equivalent to a thread.

    1. The heap is shared by all threads in the JVM, so allocating object memory on it requires locking, which also causes the overhead of new objects to be high
    2. The Sun Hotspot JVM allocates an independent TLAB (Thread Local Allocation Buffer) to the threads it creates in order to improve the efficiency of object memory Allocation. The TLAB size is calculated by the JVM according to the running situation. There is no lock required to allocate objects on TLAB, so the JVM tries to allocate memory on TLAB when allocating objects on thread. In this case, the PERFORMANCE of allocating object memory in JVM is almost as efficient as that of C, but if the object is too large, it still uses heap space allocation directly
    3. TLAB only works with the new generation of Eden Space, so when writing Java programs, it is often more efficient to allocate multiple small objects rather than large ones.

    Set up a virtual machine parameters – XX: if UseTLAB, when the thread is initialized, it will also apply for a specified size of memory, only to the current thread to use, so that each thread has a space alone, if you need to allocate memory, on your own space allocation, so there is no competition, can greatly improve the allocative efficiency.

    TLAB space memory is very small, by default only accounts for 1% of the whole Eden space, also can through the options – XX: TLABWasteTargetPercent set TLAB space occupied the percentage of Eden space size.

    TLAB is essentially three pointer managed areas ** : ** Each thread will allocate a space from Eden, such as 100KB, as its TLAB. Start and end are placeholders to identify the area managed by the TLAB in Eden. Block a space in Eden from being allocated by another thread.

    TLAB only allows each thread to have a private allocation pointer, but the memory space underneath the object is still accessible to all threads, but other threads cannot allocate it in this area. From this point of view, it makes more sense to translate it as a thread private allocation area

    When a TLAB is used up (allocation pointer TOP hits allocation limit end), a new TLAB is applied, while the objects in the old TLAB remain in place and do not care about anything — they cannot perceive whether they were allocated from TLAB before, and only care that they are allocated in Eden.

1.4 Initializing a Zero value

Now that you’ve created the object, do you want to give it a name? I (virtual machine) this is a dragon service;

Yes, the virtual machine must initialize the allocated memory space (but not the object header) to zero once the memory allocation is complete, which can also be done in advance of TLAB allocation if TLAB is used. This ensures that the instance fields of the object can be used directly in Java code without assigning initial values, enabling the program to access the zero values corresponding to the data types of these fields.

1.5 Setting the Object Header

At this point, object creation is almost over, but the virtual machine is a perfectionist, so the Java Virtual machine has to set up the necessary objects. For example, which class the Object is an instance of, how to find the metadata information about the class, the Object’s hashCode(which is actually deferred until the Object::hashCode() method is actually called), the Object’s GC generation age, etc.

This information is stored in the Object Header of the Object. The object header can be set differently depending on the VM running status, for example, whether biased locking is enabled. More on object headers later.

1.6 Running the init method

Now an object can be said to be built for me (virtual machine), but the guy (Java programmer) doesn’t recognize it! You’re just getting started. Some of my requirements haven’t been completed yet. – No blame for the lad’s high standards, there is indeed follow-up work;

After all the work is done, a new object has been created from the virtual machine’s perspective. But from a Java program’s point of view, object creation has only just begun — the constructor, the () method in the Class file, has not yet been executed, all fields have default zero values, and other resources and state information needed by the object have not yet been constructed as intended.

In general (depending on whether the new instruction is followed by the Invokespecial instruction in the bytecode stream), the Java compiler generates both bytecode instructions at the same time where the new keyword is encountered, but not necessarily if it is generated directly by other means), the new instruction is followed by the () method, The object is initialized as the programmer wishes, so that a truly usable object is fully constructed.

The memory layout of the object

(Good at drawing, just look at it)

The first thing we need to know is that objects are laid out in memory in three areas:

  • Object head
  • The instance data
  • Alignment filling

Next, we will analyze the data of objects stored in each of the three regions

2.1 object head

The Mark Word section of the HotSpot VIRTUAL machine object contains two types of information and actually three types of information

  1. One class is used to store the object itselfRuntime data
  2. One kind isType a pointer
  3. One is array length (array specific)

2.1.1 Runtime data of the object itself

The runtime data area of the object header is used to store information such as HashCode, GC generation age, lock status flags, locks held by threads, bias thread ids, bias timestamps, and so on. The length of this data is 4 bytes (32-bit) and 8 bytes (64-bit) for 32-bit and 64-bit VMS (pointer compression is enabled by default).

An object header is a dynamically defined data structure. In this way, as much data can be stored in a very small space, and the storage space can be reused based on the object status to save storage costs and vm space efficiency.

The contents stored in the object header vary in different states, as shown in the following table:

Store content Sign a state
Object hash code, object generation age 01 Unlocked state
Pointer to the lock record in the stack 00 Lightweight lock
A pointer to a mutex (heavyweight lock) 10 Heavyweight lock
No information is recorded 11 The GC tag
Bias thread ID, bias timestamp, object generation age 01 Biased lock, whether biased lock is 1

Ex. :

In a 64-bit HotSpot virtual machine, if the object is unlocked, 26 bits of the 64 bytes of the object header are idle, the HashCode has 31 bits, 4 bits to describe generational age, 1 bit fixed to zero to indicate non-bias lock, and 2 bits to store the lock flag bits.

Here is the object header structure for OS 32 and 64:

Explain some nouns:

14. HashCode: an object-like ID generated by the Hash algorithm, using equals() to compare objects for equality;

Generational age: The number of garbage collections the object has undergone. By default, an object has undergone 15 garbage collections in the new generation (generational age >15) and is old if it is still alive

The lock flag bit is used by the JVM to identify whether the object is locked and the level of lock (the JVM has three levels of biased lock, lightweight lock and heavyweight lock, depending on the lock expansion process).

2.1.2 Type Pointers

The other part of the object header is a type pointer, a pointer to the object’s type metadata, which the Java virtual machine uses to determine which class the object is an instance of.

Not all virtual machine implementations are required to keep type Pointers on object data. In other words, finding metadata information about an object does not have to go through the object itself, which we will discuss in more detail in object access positioning.

Class pointer memory size: – XX: + UseComparessedClassPointers open word is 4 bytes and don’t open is 8 bytes

2.1.3 Array Length

If the object is a Java array, there must also be a piece of data in the object header to record the length of the array, because the virtual machine can determine the size of the Java object from the metadata information of ordinary Java objects, but if the length of the array is uncertain, there is no way to infer the size of the array from the information in the metadata.

2.2 Instance Data

The next part of the instance data is the valid information that the object actually stores, that is, the contents of the various types of fields that we define in our program code, whether inherited from a parent class or defined in a subclass, must be recorded. The order in which this part is stored is affected by the virtual machine allocation policy parameter (-xx: FieldsAllocationStyle parameter) and the order in which fields are defined in the Java source code;

The default allocation order of HotSpot VMS is Longs/Doubles, INTS, SHORTS /chars, Bytes/Booleans, and Oops. As you can see from the default allocation strategy above, fields of the same width are always allocated together. If this condition is met, variables defined in the parent class will appear before the child class. If the +XX: CompactFields parameter of the HotSpot VIRTUAL machine is true (the default is true), then narrower variables in the subclass are allowed to be inserted into the gap of the parent class variables to save a little space.

Variables defined in the parent class appear before the subclass, but we can insert smaller variables in the subclass into the gap between larger variables in the parent class by setting the CompactFileds parameter to true.

2.3 Align Padding

The third part of the object is the alignment fill, which is not necessarily there and has no special meaning. It serves only as a placeholder. Since the automatic memory management system of the HotSpot VIRTUAL machine requires that the object start address be an integer multiple of 8 bytes, in other words, any object must be an integer multiple of 8 bytes;

The object header has been carefully designed to be a multiple (1 or 2) of exactly 8 bytes, so if the object instance data part is not aligned, it needs to be filled out by alignment.

The size of a pointer addressed by the CPU is 8 bytes

Alignment padding exists to improve the efficiency of CPU access to data, which is a space for time approach; Although it improves access efficiency (reduces the number of memory accesses), it wastes space.

3. Object access positioning

Objects are created to use objects, and our Java program operates on specific objects on the heap using reference data on the stack. The object access mode depends on the IMPLEMENTATION of the VM. Currently, the mainstream access mode includes ① handle and ② direct pointer:

  1. Handle: If handle is used, a chunk of memory will be allocated to the Java heap as a handle pool. Reference stores the address of the handle of the object, and the handle contains the specific address information of the instance data and type data of the object.

    Object handles contain:

    • Instance data address information
    • Object type Data Address information

  2. Direct Pointers: If direct pointer access is used, then the layout of the Java heap object must consider how to place information about the access type data, and the direct stored in Reference is the address of the object.

Both object access methods have their own advantages:

  • The biggest benefit of using handles for access is that reference stores a stable handle address and only changes the instance data pointer in the handle when the object is moved. Reference itself does not need to be modified.
  • The biggest advantage of using direct pointer access is fast speed, it saves the time cost of a pointer location.

conclusion

  • Due to the lack of knowledge of the blogger, there will be mistakes, if you find mistakes or bias, please comment to me, I will correct it.
  • If you think the article is good, your retweets, shares, likes and comments are the biggest encouragement to me.
  • Thank you for reading. Welcome and thank you for your attention.