JVM system learning path series demo code address: github.com/mtcarpenter…

preface

  • How are objects stored in the JVM?
  • What’s in the object header information?
  • What do Java object headers have?

Instantiation of an object

Object creation mode

  • New: the most common method, called in a singleton class
    • Variant 1: static class method of getInstance
    • Transformation 2: static method of XXXBuilder/XXXFactory
  • Class newInstance method: marked as obsolete in JDK9 because only the empty parameter constructor can be called
  • Constructor newInstance(XXX) : a reflective way of calling either an empty parameter or a Constructor with a parameter
  • Using Clone () : Does not call any constructor, requiring the current class to implement the Clone interface in the Cloneable interface
  • Use serialization: Serialization is generally used for Socket network transmission
  • Third-party library Objenesis

Steps for creating objects

To determine whether an object’s corresponding class is loaded, linked, and initialized to a new instruction, first check whether the instruction’s parameters can locate a symbolic reference to a class in Metaspace’s constant pool, and check whether the symbolic reference represents a class that has been loaded, parsed, and initialized. (that is, to determine whether the class meta information exists). If not, then in parent delegate mode, use the current ClassLoader to find the corresponding.class file with ClassLoader + package name + class name key. If not, throw ClassNotFoundException. If not, throw ClassNotFoundException. Class load is performed and the corresponding Class object is generated.

Allocates memory for objects

You calculate how much space an object occupies, and then you allocate a chunk of memory in the heap for the new object. If the instance member variable is a reference variable, only the reference variable space is allocated, which is 4 bytes

  • If memory is neat: pointer collision
  • If the memory is not tidy
    • Virtual tables need to maintain a list
    • Free list allocation

If The memory is regular, The virtual machine will use a method called Bump The Point to allocate memory for objects. This means that all the used memory is on one side, the free memory is on the other, and a pointer is placed in the middle as a pointer to the dividing point. Allocating memory simply moves the pointer to the free memory by a distance equal to the size of the object. If the garbage collector chooses Serial, ParNew, which is based on the compression algorithm, the virtual machine uses this allocation. Pointer collisions are typically used with collectors that have a Compact process. If memory is not tidy and used memory and unused memory are interlaced, the virtual machine will use the free list to allocate memory for objects. The virtual machine maintains a list of memory blocks that are available, finds a large enough space in the list to allocate to object instances, and updates the list. This allocation becomes the “Free List.” The choice of allocation depends on whether the Java heap is clean, and whether the Java heap is clean depends on whether the garbage collector is compacted or not.

Dealing with concurrency

  • CAS is used to configure failed retries to ensure atomicity of updates
  • Pre-allocated TLAB per thread – set by setting the -xx :+UseTLAB parameter (region locking mechanism)
    • Assign an area to each thread in the Eden area

Initialize the allocated memory

The operation that assigns a value to an object property

  • Default initialization of the property
  • Display initialization
  • Initialization in a code block
  • Constructor initialization
  • All properties are set to default values, ensuring that object instance fields can be used directly without assigning values

Sets the object header of the object

The object’s owning class (that is, the metadata information of the class), the object’s HashCode, and the object’s GC information, lock information and other data are stored in the object’s object header. How this process is set up depends on the JVM implementation. From a Java program’s point of view, initialization begins. Initialize the member variable, execute the instantiation code block, call the constructor of the class, and assign the first address of the heap object to the reference variable. Therefore, in general (as determined by the Invokespecial instruction in the bytecode), the new instruction is followed by the execution of the method, which initializes the object as the programmer wishes. Only then will a truly usable object be created.

The process of object instantiation

  • Load class meta information
  • Allocates memory for objects
  • Dealing with concurrency
  • Default initialization of properties (zero value initialization)
  • Set the object header information
  • Display initialization of a property, initialization in a code block, initialization in a constructor

Object memory layout

The object header contains two parts, the runtime metadata (Mark Word) and the type pointer.

If it is an array, you also need to record the length of the array

Runtime metadata

  • Hash values (HashCode)
  • GC generational age
  • Lock status flag
  • The lock held by the thread
  • Biased thread ID
  • Plane to the time stamp

Type a pointer

  • Hash values (HashCode)
  • GC generational age
  • Lock status flag
  • The lock held by the thread
  • Biased thread ID
  • Plane to the time stamp

Instance Data descriptionIt’s not necessary, it has no special meaning, it’s just a placeholdersummary How does the JVM access its internal object instances through object references in the stack frame?

Object access in two ways

Handle access

Handle access means that references to objects are recorded in the local variable table of the stack, and then a space is created in the heap space, called the handle pooladvantagesReference stores stable handle addresses. When objects are moved (which is common in garbage collection), only the instance data pointer in the handle is changed. Reference itself does not need to be modifiedDirect Pointers (adopted by HotSpot)

A direct pointer is a reference in the local variator table that points directly to the instance in the heap, and a type pointer in the object instance that points to the object type data in the method area


Welcome to pay attention to the public number Shanma carpenter, I am Xiao Chun brother, engaged in Java back-end development, will be a little front-end, through the continuous output of a series of technical articles to literary friends, if this article can help you, welcome everyone to pay attention to, like, share support, we see you next period!