1. Create a VM object

At the language level, object creation is usually just a new keyword (exception: copy, deserialization). The virtual machine object creation discussed in this article does not include arrays, Class objects, etc., for ordinary objects.

  1. Locate symbol references in the constant pool

    • When the Java virtual machine reaches a bytecode new instruction, it checks whether the instruction’s argument can locate a symbolic reference in the constant pool.
  2. Check that the class is loaded

    • Check whether the class represented by this symbolic reference has been loaded, parsed, and initialized.
  3. Class loading process

    • Determine the memory occupied by the loaded class and divide the space for storage
    • There are two ways to divide space
      • Pointer collision, heap memory tidy
        • The garbage collectors represented are Serial, ParNew, and the algorithm mark-collation algorithm
        • May cause thread unsafe issues, see below tip1: Thread safety issues caused by pointer collisions.
      • The free list
        • The garbage collector, CMS, which represents the garbage collector, uses a tag clearing algorithm
    • Object to set
      • The e virtual machine also makes necessary Settings on the object, such as which class the object is an instance of, how to find the metadata information about the class, the object’s hash code, the object’s GC generation age, and so on. This information is stored in the object header of the object (Tip2: objects store content).

    After the above 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 is just beginning — the constructor, the () method in the Class file, has not yet been executed, and all fields have default zero values. After combing through the virtual machine object creation process, take a look at the storage layout of objects in the heap memory.

2. Memory layout of the object

In the HotSpot virtual machine, the storage layout of objects in the heap memory can be divided into three parts: object headers, Instance Data, and Padding.

2.1 object head

The object header of the HotSpot VIRTUAL machine object contains two types of information:

  1. The first type is the runtime data used to store the object itself, such as HashCode, GC generation age, lock status flags, locks held by threads, biased thread IDS, biased time stamps, etc. Officially called “Mark Word”.
  2. Second type of pointer

2.2 Instance Data

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

The default allocation order of HotSpot VIRTUAL machines 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.

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.

2.3 Align Padding

Since the HotSpot VIRTUAL machine’s automatic memory management system requires that the object’s starting address be an integer multiple of 8 bytes, in other words, any object’s size 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.

In general, “data items can only be stored in memory locations where the address is an integer multiple of the size of the data item (even address, address divisible by 4, address divisible by 8).” For example, an int takes up four bytes, and the address can only be in positions 0, 4, and 8.

To be good at alignment padding, you really have to understand the c++ implementation.

Case 1:

#include <stdio.h>
struct xx{
    char b;
    int a;
    int c;
    char d;
};   // The overall structure takes 16 bytes

struct xx{
        char b; 
        char d;
        int a;          
        int c;                  
};	// The overall structure takes 12 bytes
Copy the code

Just say the conclusion, and the procedure can print out the address of the due field.

You’ll notice that b will be filled with 3 bytes, d will be filled with 3 bytes, and that’s where the alignment is filled, and in your code, you need to do a good job sorting, you need to pay attention to alignment.

Tip1: Thread-safety issues caused by pointer collisions.

Object creation is A very frequent activity in virtual machines. Even if only one pointer is changed, it is not thread-safe in concurrent situations. Object B may use the original pointer to allocate memory before the pointer is changed. There are two possible solutions to this problem:

  1. One is to synchronize the memory allocation — virtual machines actually use CAS and retry to ensure atomicity of the update operation.
  2. In other words, each Thread allocates a small chunk of memory in the Java heap in advance, called Thread Local AllocationBuffer (TLAB). Which Thread should allocate memory? It is allocated in the local buffer of the thread, and only needs to be synchronized when the local buffer is used up and a new buffer is allocated. The -xx: +/ -usetlab parameter is used to determine whether the VM uses TLAB.

Tip2: Object headers store content

Object headers are also called “Mark words “. Defines a data structure with dynamic data such as 2 out of 32 bits of Mark Word storage space in a 32-bit HotSpot VIRTUAL machine where the object is not locked by a synchronization lockFive bits are used to store object hash codes.Four bits are used to store the generational age of the object.Two bits are used to store the lock flag bit.One bit is fixed to 0The contents of objects stored in other states (lightweight lock, heavyweight lock, GC mark, biased) are shown in the table.The other part of the object header is the type pointer, the pointer to the object’s type metadata, which the Java virtual machine uses to determine which class the object is an instance of.