jvm composition

quote
http://thw.iteye.com/blog/953869

1 Introduction
to JVM JVM is the most basic foundation of our Javaer. When we first learn Java, we usually start with "Hello World", then we will write a complex class, and then find some open source frameworks, such as Spring and Hibernate. Wait, and then develop enterprise-level applications, such as websites, internal enterprise applications, real-time transaction systems, etc., until one day I suddenly realize that the system is so slow, and there is a memory overflow from time to time. Today The trading system reported a StackOverflowError, and tomorrow the website system reported an OutOfMemoryError. This kind of error is difficult to reproduce. Only by analyzing the Javacore and dump files, if you are lucky, you can also analyze the result. Burn incense in the temple! Every day I take calls from customers with trepidation, for fear of what will happen again. I think Java has been doing this for a long time, so what is the ultimate root cause of these problems? - JVM.
The full name of JVM is Java Virtual Machine, which is to virtualize a computer on the computer. This is different from using VMWare. You can see that virtual thing. You cannot see this JVM. exists in memory. We know that the basic components of a computer are: arithmetic unit, controller, memory, input and output devices, then the JVM also has this set of elements, the arithmetic unit is of course handed over to the hardware CPU and processed, just to adapt to the "one-time compilation" , run anywhere", a translation action is required, so the JVM's own command set is used, which is somewhat similar to the assembly command set. Each assembly command set is aimed at a series of CPUs, such as the 8086 series assembly. It can be used on 8088, but it can't run on 8051, and the command set of JVM can be run everywhere, because JVM has done translation, and translated into different machine languages ​​according to different CPUs.
The most in-depth understanding of the JVM is its storage part, storage? hard disk? NO, NO, JVM is a virtual machine in memory, then its storage is memory. All the classes, constants, variables, and methods we write are in memory, which determines whether our program runs robustly and efficiently. The next part is to focus on it.
2 Components of the JVM
We first draw the virtual machine JVM, as shown in the following figure:




As can be seen from this figure, the JVM runs on the operating system, and it has no direct interaction with the hardware. Let's take a look at the components of the JVM, as shown in the following figure:


This figure refers to the widely circulated JVM composition diagram on the Internet. Look at this diagram. The entire JVM is divided into four parts:
q Class Loader Class Loader
Class The function is to load the class file into the memory, such as writing a HelloWord.java program, and then compile it into a class file through javac, how can it be loaded into the memory and executed? Class Loader undertakes this responsibility. It is impossible to create a .class file and load it. The class file loaded by Class Loader has format requirements. The structure of the Class file is defined in the "JVM Specification" as follows:
    ClassFile {
      u4 magic;
      u2 minor_version;
       u2 major_version;
      u2 constant_pool_count;
      cp_info constant_pool[constant_pool_count-1];
      u2 access_flags; u2
      this_class;
      u2 super_class;
      u2 interfaces_count;
      u2 interfaces[interfaces_count];
      u2 fields_count;
      field_info fields[fields_count];
      u2 methods_count;     method_info
      methods[methods_count]; u2
      attributes_count;
      attribute_info attributes[attributes_count]; , you can read the fourth chapter "The class File Format" of the "JVM Specification" carefully, and will not be described in detail here. Friendly reminder: Class Loader only loads, as long as it conforms to the file structure. As for whether it can run or not, it is not responsible for it, but the Execution Engine is responsible for it. q Execution Engine Execution Engine The execution engine is also called the Interpreter, which is responsible for interpreting the commands and submitting them to the operating system for execution. q Native Interface local interface






The function of the local interface is to integrate different programming languages ​​for Java. Its original intention is to integrate C/C++ programs. When Java was born, when C/C++ was rampant, in order to gain a foothold, there must be a smart and wise call C /C++ program, so an area is specially opened in the memory to process the code marked as native. Its specific method is to register the native method in the Native Method Stack and load the native libraies when the Execution Engine executes. At present, this method is used less and less, unless it is a hardware-related application, such as driving a printer through a Java program, or managing a production device with a Java system. The communication is very developed, for example, you can use Socket communication, you can also use Web Service, etc., so I won't introduce it much.
q Runtime data area Runtime data area
Runtime is the focus of the entire JVM. All the programs we write are loaded here before they start running, and the Java ecosystem is so thriving thanks to the good autonomy of the region, which is described in detail in the next chapter.

The entire JVM framework is loaded by the loader, and then the executor processes the data in memory. It needs to interact with the heterogeneous system through the local interface, and voila, a complete system is born!
2 JVM memory management
All data and programs are stored in the running data area, which includes the following parts:
q Stack The
stack , also called stack memory, is the running area of ​​a Java program, created when a thread is created, and its life The period is the life period of the following thread, and the stack memory will be released when the thread ends. There is no garbage collection problem for the stack. As long as the thread ends, the stack will be Over. The question arises: what data is stored in the stack? What is the format?
The data in the stack exists in the format of a stack frame (Stack Frame). The stack frame is a memory block, a data set, and a data set related to methods (Method) and runtime data. When a method A is called When the call is made, a stack frame F1 is generated and pushed into the stack. The A method calls the B method, so the generated stack frame F2 is also pushed into the stack. After the execution is completed, the F2 stack frame is first popped, and then F1 is popped. Stack frames follow the "first in, last out" principle.
What data actually exists in the stack frame? The stack frame mainly saves three types of data: local variables (Local Variables), including input parameters and output parameters and variables in the method; stack operations (Operand Stack), recording operations of popping and pushing into the stack; stack frame data (Frame Data ), including class files, methods, etc. It's boring to talk about it, let's draw a picture to understand the Java stack, as shown in the following figure:


There are two stack frames in a stack, and stack frame 2 is the first method to be called. Method 1 is called, stack frame 1 is at the top of the stack, and stack frame 2 is at the bottom of the stack. After the execution is completed, stack frame 1 and stack frame 2 are popped out in turn, the thread ends, and the stack is released.
q Heap heap memory There is only one heap class memory for
a JVM instance, and the size of the heap memory can be adjusted. After the class loader reads the class file, it needs to put the classes, methods, and constant variables into the heap memory to facilitate the execution of the executor. The heap memory is divided into three parts:
Permanent Space Permanent storage area
Permanent storage area is a resident memory The area is used to store the metadata of the Class and Interface carried by the JDK itself, that is to say, it stores the class information necessary for the running environment. The data loaded into this area will not be reclaimed by the garbage collector. The JVM will release the memory occupied by this area.
Young Generation Space
The new area is the area where classes are born, grow, and die. A class is generated here, applied, and finally collected by the garbage collector, ending its life. The new area is divided into two parts: Eden space (Eden space) and survivor area (Survivor pace), all the classes are new in the Eden area. There are two survival areas: 0 area (Survivor 0 space) and 1 area (Survivor 1 space). When the space in the Garden of Eden is used up, the program needs to create objects again. The garbage collector of the JVM will perform garbage collection on the Garden of Eden and destroy the objects in the Garden of Eden that are no longer referenced by other objects. Then move the remaining objects in the Garden of Eden to Survival Zone 0. If the surviving area 0 is also full, the area is garbage collected, and then moved to area 1. What if zone 1 is also full? Then move to the retirement area.
Tenure generation space Tenure generation space The
retirement is used to save the JAVA objects selected from the new area. Generally, pool objects are active in this area. The schematic diagram of the three areas is as follows:


q Method Area Method area The
method area is shared by all threads. This area saves all fields and method bytecodes, as well as some special methods such as constructors, and the interface code is also defined here.
q PC Register program counter
Each thread has a program counter, which is a pointer to the method bytecode in the method area, and the next instruction is read by the execution engine.
q Native Method Stack Native method stack

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326646927&siteId=291194637