Detailed explanation of JVM object creation and access positioning process
java Basic TutorialThe column introduces the process of JVM creating objects and accessing positioning
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
