Home > Java > JavaBase > Detailed explanation of JVM object creation and access positioning process

Detailed explanation of JVM object creation and access positioning process

coldplay.xixi
Release: 2020-12-08 17:50:03
forward
3105 people have browsed it

java Basic TutorialThe column introduces the process of JVM creating objects and accessing positioning

Detailed explanation of JVM object creation and access positioning process

Related free learning recommendations: java basics Tutorial

1. Object creation

  • When the virtual machine receives the new instruction, it checks whether this instruction can locate a class symbol in the constant pool. reference, and checks whether the class represented by this symbolic reference has been loaded, resolved, and initialized. If there are none, perform the class loading process first.
  • After the class loading is passed, the virtual machine allocates memory for the new object (dividing a memory of a certain size from the Java heap). The memory size can be completely determined after the class loading is completed.
  • Two allocation methods:
    • (1): Pointer collision: Assume that the memory in the Java heap is absolutely regular, that is, the used memory is on one side, the free memory is on the other side, and the middle A pointer is placed as an indicator, and memory allocation is achieved by moving the pointer.
    • (2): Free list: If the memory in the Java heap is not regular, that is, used memory and free memory are interleaved, the virtual machine must maintain a list to record which memory blocks are available. , allocates memory by finding space from the list to allocate to object instances.
  • Whether the Java heap is regular or not is determined by whether the garbage collector used has a compression and finishing function.
  • Creating objects in a virtual machine is not a thread-safe behavior. It may occur when object A is allocated memory and the pointer has not had time to be modified, and object B uses the original pointer to allocate memory. There are two solutions:
  • (1): Synchronize the action of allocating memory space. In fact, the virtual machine uses CAS with failure retry to ensure the atomicity of the update operation;
  • (2): The memory allocation action is divided into different spaces according to threads, that is, each thread pre-allocates a small piece of memory in the Java heap, called the local thread allocation buffer (Thread Loal Allocation Buffer, TLAB) .
  • After the memory allocation is completed, the allocated memory space needs to be initialized to zero values ​​to ensure that the instance fields of the object can be used directly in the Java code without assigning initial values, and the program can access these fields. The zero value corresponding to the data type.
  • Set the object, store which class the object is an instance of, how to find the metadata information of the class, the hash code of the object, the GC generation age of the object, etc. in the object header.

2. Memory layout of objects: The layout of objects stored in memory can be divided into three parts: object header (Header), instance data (Instance Data), and alignment padding (Padding). The object header includes two parts of information:

(1): Stores the runtime data of the object itself, such as hash code, GC generation age, lock status flag, lock held by the thread, and bias Thread ID, bias timestamp, etc. The length of this part of data is 32bit and 64bit in 32-bit and 64-bit virtual machines respectively. It is officially called Mark Word (a non-fixed data structure that multiplexes its own storage according to the state of the object). space).

#(2): Type pointer, that is, a pointer to the class metadata of the object. The virtual machine uses this pointer to determine which class the object is an instance of.

  • Instance data: The effective information actually stored by the object, that is, the contents of various types of fields defined in the program code. Whether it is inherited from the parent class or defined by the subclass itself, it needs to be recorded.
  • Alignment padding: It does not necessarily exist and serves as a placeholder. Since HotSpot VM requires that the size of the object must be an integer multiple of 8 bytes, and the object header part is exactly an integer multiple of 8 bytes, Therefore, when the instance data is not aligned, it is completed by alignment padding.

3. Object access positioning: Java operates specific objects on the heap through reference data on the stack (object references in local variable tables). Reference only specifies references to objects, not Define how to locate and access the location of objects in the heap. The object access method is implemented by Swift.

(1): Handle access: The Java heap will divide a piece of memory as a handle pool. The reference stores the handle address of the object. The handle contains the address information of the object instance data and type data.

Advantages: The reference stores a stable handle address. When the object moves, only the instance data pointer in the handle changes, not the reference.

(2): Direct pointer: What is stored in the reference is directly the object address, and the address of the accessed object type data (stored in the method area) is placed in the Java heap.

Advantages: Faster, saving the time overhead of pointer positioning. HotSpot uses direct pointer access.

The above is the detailed content of Detailed explanation of JVM object creation and access positioning process. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template