Understand the java virtual machine

Speaking of java, I have to mention a very important role, that is the Java virtual machine. So what is the java virtual machine?

The Java virtual machine is an abstract computer that runs all Java programs, is the runtime environment of the Java language, and is a virtual machine process that can execute Java bytecodes. Java source files, which are .java files, are compiled into bytecode files that can be executed by the Java virtual machine, which are .class files. Java can run on the operating platform only if the Java Virtual Machine is installed.

Know what is the java virtual machine, then what features does it have?

One of the biggest features is cross-platform operation. The java operation process is generally: source program --> compilation --> bytecode --> bytecode interpreter --> the machine language of the corresponding platform.

The architecture of the java virtual machine, as shown in the following figure

 

 

It is divided into class loader (used to load .class files), runtime data area (method area, heap, java stack, program register, native method stack), execution engine (execute bytecode, or execute native method).

The following is an understanding of the jvm runtime data area:

1. Program Counter

 The program counter is a small memory space that is private to the thread. It can be regarded as the line number indicator of the bytecode executed by the current thread. The work of the bytecode interpreter is to select the next bytecode instruction to be executed by changing the value of this counter.

If the thread is executing a java method, then this counter records the address of the virtual machine bytecode instruction being executed; if it is executing a Native method, the value of this counter is empty. This memory area is the only one that does not specify any OutOfMemoryError condition in the Java Virtual Machine Specification.

2. Java virtual machine stack

Is the memory model that describes the execution of java methods, and is also thread-private. When each method is executed, a stack frame is created to store the local variable table, operand stack, dynamic link, method exit and other information. The process of each method from invocation to completion of execution corresponds to the process of a stack frame being pushed to the stack in the virtual machine stack.

A stack frame is a data structure used to store data and partial process results, and is also used to handle dynamic linking, method return values, and exception dispatch. Stack frames are created when a method is called and destroyed when the method ends.

3. Native method stack

Similar to the Java virtual machine stack, threads are private. The difference is that the Java virtual machine stack serves the execution of Java methods (bytecode), and the native method stack serves the native methods (Native) used by the virtual machine. Some virtual machines (such as the Sun HotsPot virtual machine) directly combine the native method stack and the virtual machine stack into one.

4. Java heap

For most applications, the Java heap is the largest piece of memory managed by the Java Virtual Machine. The Java heap is a memory area shared by all threads. The created objects and arrays are kept in the java heap memory, which is also the most important memory area for garbage collector to collect. Since modern VMs use a generational collection algorithm, the Java heap can also be subdivided from the perspective of GC: the new generation and the old generation. If there is no memory in the heap to complete the instance allocation, and the heap cannot be expanded, an OutOfMemoryError exception will be thrown.

5. Method area

It is shared between threads and is used to store data such as class information, constants, static variables, code compiled by the real-time compiler, etc. loaded by the JVM. HotSpot VM extends the GC generational collection to the method area, that is, uses the permanent generation of the Java heap to Implement the method area, so that HotSpot's garbage collector can manage this part of the memory like the Java heap, without having to develop a special memory manager for the method area (the main goal of memory reclamation in the permanent generation is to recycle and type the constant pool offloading, so the benefit is generally small).

The runtime constant pool is a part of the method area. In addition to the class version, field, method, interface description and other information in the Class file, there is also a constant pool. It is used to store various literals and symbolic references generated during compilation. This part of the content will be stored in the runtime constant pool of the method area after the class is loaded. The Java virtual machine has strict regulations on the format of each part of the Class file (including the constant pool, of course). What kind of data each byte is used to store must meet the requirements of the specification, so as to be recognized by the virtual machine, Load and execute.

6. Direct Memory

Direct memory is not part of the virtual machine runtime data area, but this part of memory is frequently used and may also cause OutOfMemoryError exceptions.

The size is not limited by the java heap size, it is limited by the native (server) memory.

 

 

Finally, I quoted a summary in a blog, which is also very vivid, as follows

1 A virtual machine is not mysterious, it is just an ordinary process from the point of view of the operating system.

2 This process called the virtual machine is special, it can load the class file we wrote. If the JVM is compared to a person, then the class file is the food we eat.

3 The class file is loaded by a subsystem called the class loader. Just like our mouths, we eat food into our stomachs.

4 The execution engine in the virtual machine is used to execute the bytecode instructions in the class file. Just like our stomach, it digests the food we eat.

5 During the execution of the virtual machine, it needs to allocate memory to create objects. When these objects become obsolete and useless, these useless objects must be cleaned up automatically. The task of cleaning up objects to reclaim memory is the responsibility of the garbage collector. Just like the food that people eat, after being digested, the waste must be excreted out of the body, making room for eating and digesting the food the next time you are hungry.

 

The above content may be very abstract, and I don't fully understand it. I just read the relevant articles myself, summarize it, and look back later, I may understand it better. The reference link is as follows:

https://www.cnblogs.com/yixianyixian/p/7680321.html

https://www.cnblogs.com/IUbanana/p/7067362.html

"In-depth understanding of Java virtual machine JVM advanced features and best practices" book.

 

Guess you like

Origin blog.csdn.net/cjy_win/article/details/88651776