Home>Article>Java> Detailed explanation of JAVA virtual machine (JVM) (2) - memory division

Detailed explanation of JAVA virtual machine (JVM) (2) - memory division

王林
王林 forward
2019-08-24 13:44:43 3387browse

We know that in C language, if you want to use an object, you need to perform a new operation on it; if you no longer use the object, you need to perform a delete operation on it. Once the developer forgets to write the delete statement, it will cause a memory leak. [If memory is occupied by an object and is not returned, it is called a memory leak. 】

But Java is smart. It has evolved from "manual" to "automatic" and handed over the control of memory to the virtual machine. Let's take a peek at how jvm performs automatic memory management.

Detailed explanation of JAVA virtual machine (JVM) (2) - memory division

Automatic memory management is divided into two parts:

Allocate memory to objects and reclaim memory allocated to objects. In this article we talk about the former, which is memory partitioning and memory allocation. Let’s talk about GC (garbage collection) in the next article.

1. Memory division

Let’s take a look at what is in the virtual machine memory. The memory area of the JVM is roughly divided into Class files, class loading subsystem, runtime data area, and execution engine. Today we only talk about the runtime data area. [This picture is based on JDK7. Before JDK7, the constant pool was stored in the method area. Since JDK7, the constant pool has been placed on the heap. 】

Detailed explanation of JAVA virtual machine (JVM) (2) - memory division

Thread public

In the runtime data area, the method area and heap are public to the thread, that is, this The two areas are "recycled", so they need to be garbage collected. It is created when the virtual machine starts.

Thread private

The virtual machine stack, local method stack, and program counter are private to the thread. They "live and die" with the thread and are "one-time" , so there is no need to garbage collect it.

(1) Method area

stores class information, constants, static variables, code compiled by the just-in-time compiler and other data that have been loaded by the virtual machine .
There is a runtime constant pool. What it stores is the symbol reference described in the Class file, a direct reference. New constants can be put into this pool at both compile time and run time.

(2) Heap

Concept: If the stack solves the problem of program operation, that is, how the program processes data; then the heap solves It is a data storage problem, that is, how and where to put the data.

Features:

a. The heap is the largest piece of virtual machine memory, accounting for about three-quarters of the memory. For example, if each process on a 32-bit Windows platform has 2GB of memory, 1.5GB of memory is generally allocated to the heap. It can be seen that the heap takes up a lot of space.
b. It can be in physically discontinuous memory space, as long as it is logically continuous.

Function:

Storage object instances, almost all object instances allocate memory here.

Classification:

From the perspective of memory recycling, it is divided into the new generation and the old generation.
From the perspective of memory allocation, multiple thread-private allocation buffers can be divided.

(3) Virtual machine stack

#The virtual machine stack stores stack frames, and the stack frames store local variable tables. Operand stack, dynamic link, method exit and other information.

Detailed explanation of JAVA virtual machine (JVM) (2) - memory division

Stack frame in the stack

Each method will create a stack frame and a method while executing The process from call to completion of execution corresponds to the process from pushing a stack frame into the virtual machine stack to popping it out.

The local variable table in the stack frame

stores various basic data types, object references, and returnAddress types that are known at compile time. Therefore, the memory space required can be allocated during compilation, and its size will not change during runtime.

When allocating the space occupied by basic data types, except for 64-bit long and double type data, which will occupy two local variable spaces, the other data types only occupy one.

(4) Local method stack

The local method stack and the virtual machine stack have the same function, except that the virtual machine stack executes java methods , the native method stack executes the Native method.
The java method is the java code written by the developer, and the Native method is an interface for java to call non-java code.

(5) Program counter

The program counter stores the line number of the bytecode executed by the current thread. When the jvm works, it selects the next bytecode instruction that needs to be executed by changing the value of this counter.

2. Memory allocation

#In this part we talk about how objects are allocated, laid out and accessed in the java heap. And the principles of memory allocation.

Creation of objects

We use new to create objects, let’s see what the virtual machine is doing when the system runs to new . At this time, humans are like a piece of meat. They have to go through layers of security checks before they can reach the human dinner table. Step 1: Check whether there is a corresponding symbol reference in the constant pool. [Proceed in the method area]

Step 2: Check whether this class has been loaded, parsed and initialized. [Proceed in the method area]

Step 3: Receive the memory of the new object. There are two ways: pointer collision and free list. [Proceed in the heap]

Step 4: Initialize the allocated memory space to zero value.

Step 5: Make necessary settings for the object, such as which class it is an instance of, the hash code of the object, etc. This information is stored in the object header of the object

Step 6: If the object is assigned an initial value in the java code, step 6 will be performed: execute the method. The purpose of this method is to initialize the object.

Memory layout of objects

The storage layout of objects in memory is divided into 3 Part: Object header instance data alignment and filling

Object header

There are two parts of information in the object header:

(1) Runtime data, including Hash code, GC generation age, lock status flag, etc.

(2) Type pointer, the virtual machine uses this pointer to determine which class this object is an instance of.

Instance data

Instance data stores the contents of various types of fields defined in the code.

Alignment padding

Alignment padding functions as a placeholder and does not necessarily exist. It only needs to ensure that the size of the object is an integer multiple of 8 bytes.

Object access location

After creating the object, we can use the object. When using it, how can you find the object you are looking for? There are two ways: handle and direct pointer

Handle:

Handle access is to divide a piece of memory in the java heap As a handle pool, the handle contains the specific address information of object instance data and type data.

Detailed explanation of JAVA virtual machine (JVM) (2) - memory division

Direct pointer:

The reason why direct pointer is "direct" is It's because it removes the intermediary of the handle. So it is faster than handle. In the HotSpot virtual machine, this method is used.

Detailed explanation of JAVA virtual machine (JVM) (2) - memory division

After talking about how objects are allocated, laid out and accessed in the java heap, let’s talk about memory allocation Principles

Principles of memory allocation:

Detailed explanation of JAVA virtual machine (JVM) (2) - memory division

##The heap is roughly divided into the new generation, the old generation, and the permanent generation. The memory allocation of objects is mainly allocated in the Eden area of the new generation, and in a few cases, it is allocated directly to the old generation. The allocation rules are not 100% fixed and depend on the garbage collector combination and parameter settings. There are several allocation principles for reference below.

(1) Objects are allocated in Eden first.

(2) Large objects enter the old age directly.

(3) Long-term surviving objects will enter the old generation.

(4) Dynamic object age determination.

(5) Space allocation guarantee.

The above is the division of memory in the JAVA virtual machine. For more questions, please visit the PHP Chinese website:

JAVA Video Tutorial

The above is the detailed content of Detailed explanation of JAVA virtual machine (JVM) (2) - memory division. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete