This is the 7th day of my participation in Gwen Challenge

The following diagram shows the Java virtual machine infrastructure, and the overall execution flow of a Java program can also be explained using this diagram.

Noun explanation:

[1. Class loading subsystem]

The class files generated by our compiled source programs first pass through this classloading subsystem, which is responsible for loading class information from the file system or network. The loaded information is stored in a memory space called the method area.

[2. Method area]

Stores class information, constant information, constant pool information, including string literals and numeric constants.

[3. Java heap]

The Java heap is established when the Java virtual machine is started. It is the main memory working area of the Java program. Almost all object instances are stored in the Java heap, saving all the real information of reference data types.

[4. Direct memory]

Java’s NIO library improves performance by allowing Java programs to use direct memory, which is usually faster than the Java heap. You may want to use it when reading and writing frequently.

[5. Java stack]

Thread privacy. a thread’s Java stack is created when the thread is created. The Java stack holds local variables, method parameters, simultaneous Java method calls, return values, etc., i.e. basic data, operations, and references to heap memory.

[6. Local method stack]

The local method stack is very similar to the Java stack, with the biggest difference being that the local method stack is used for local method calls. Java virtual machines allow Java to call native methods directly (usually written in C or C++).

[7. Garbage Collection system]

Garbage collection system is the core of Java, is also essential, Java has its own garbage cleaning mechanism, through the garbage collector GC to recycle memory release.

[8.PC register]

The PC (Program Counter) register is also a private space for each thread. The Java virtual machine creates a PC register for each thread. At any time, a Java thread is always executing a method, which is called the current method. The PC register will execute the instruction currently being executed. If it is a local method, the PC register value is undefined, and the register stores information such as: current execution environment pointer, program counter, operation stack pointer, computed variable pointer, and so on.

[9. Execution Engine]

The execution engine is the most core component of a virtual machine. It is responsible for the execution of the virtual machine bytecode, which is first compiled into machine code and then executed by ordinary users.

What are stored in the heap and stack respectively?

Objects are stored in the heap.

The stack holds references to basic data types and objects in the heap.

In Java, each thread has a corresponding thread stack, because different threads execute logic differently and therefore need a separate thread stack.

The heap is shared by all threads.

Stack is a running unit, so the information stored inside is related to the current thread (or program) information, including local variables, program running status, method return value, while the heap is only responsible for storing object information.

Why not put basic types in the heap as well?

Because basic types typically take up 1 to 8 bytes of space, they require less space, and because they are basic types, there is no dynamic growth (the length is fixed).

【 Concept and relation of heap, stack and method area 】

[heap]

The heap solves the problem of data storage, where and where data is stored.

Stack []

The problem the stack solves is the problem of program execution, that is, how the program executes, or how it processes data.

[Method area]

Also known as static area, also thread sharing. The method area is the permanent area (Perm) of the secondary stack, which addresses the generation of stack information and is a prerequisite.

Example to understand

Create a new object User: then some information about the User class (class information, static information) is stored in the method area,

After the User object is instantiated, it is stored in the Java heap, a chunk of memory.

When used, we use references to User objects (e.g. User u1 = new User();).

The User in this case is a reference to the User real object that is stored in the Java stack.

【 Java heap 】

The Java heap is the memory space most closely related to Java applications, where almost all objects are stored, and the Java heap is completely automated. Through the garbage collection mechanism, garbage objects are automatically cleaned up without being explicitly released.

Depending on the garbage collection mechanism, the Java heap may have a different structure. The most common is to divide the entire Java heap into new generation and old generation.

Among them, the new generation stores the new object, and the old age stores the old object.

The Cenozoic era is divided into Eden, S0 and S1 regions.

S0 and S1, also known as the FROM and to zones, are two Spaces of the same size that swap roles.

In most cases, the object will be allocated to Eden area first. After the next generation recycling, if the object is still alive, it will enter S0 or S1 area. After each generation recycling, if the object is alive, its age will increase by 1.

“Java stack”

The Java stack is a thread-private memory space, a stack, generally composed of three parts: local variables, operand stack, frame data area.

[Local variable scale]

Save the parameters and local variables of the error method.

[Operand stack]

Store the intermediate results of the calculation process and serve as temporary storage space for variables during the calculation process.

[Frame data area]

In addition to the local variables and the operand stack, stack also need some data to support the constant pool parsing, here the frame data area holds a pointer to a constant pool, convenient access constant pool program, in addition, when the method returns or abnormal, the virtual machine must have an exception handling table, convenient send abnormal find abnormal code. Therefore, the exception handling table is also part of the frame data area.

【Java Methods section 】

The Java method area, like the heap, is an area of memory shared by all threads that holds information about the system’s classes, such as their fields, methods, constant pools, and so on.

The size of the method area determines how many classes the system can hold. If the system defines too many classes and the method area overflows, the virtual machine will also throw an out-of-memory error. The method region can be understood as the permanent region (Perm).