What is the principle of JVM?
Firstly, two concepts are clarified here: JVM instance and JVM execution engine instance. JVM instance corresponds to an independently running Java program, while JVM execution engine instance corresponds to a thread belonging to the user running the program. That is, the JVM instance is at the process level and the execution engine is at the thread level. What is JVM? -the life cycle of JVM The birth of JVM instance: When a Java program is started, a JVM instance is generated, and any class with the publicstaticvoidmain (string [] args) function can be used as the starting point for the JVM instance to run. In this case, how does the JVM know that it is running the main of classA instead of the main of classB? This requires explicitly telling the JVM the class name, that is, the origin of the commands that we usually run Java programs, such as JavaclassAhelloworld, where Java is the Java virtual machine that tells the os to run SunJava2SDK, and classA points out the class name needed to run the JVM. Running of JVM instance: main () is the starting point of the initial thread of the program, and any other thread is started by this thread. There are two kinds of threads in JVM: guardian thread and non-guardian thread. main () belongs to non-guardian thread, and the guardian thread is usually used by JVM itself. Java programs can also indicate that the thread they create is a guardian thread. The death of JVM instance: the JVM will quit when all non-guardian threads in the program are terminated; If the security manager allows, the program can also use the Runtime class or System.exit () to exit. What is JVM? The architecture of —JVM is roughly divided into three parts: ClassLoader subsystem, runtime data area and execution engine. The following will introduce the class loader first, then the execution engine, and finally the runtime data area 1 and the class loader, which, as the name implies, are used to load. class files. There are two kinds of JVM loaders: the startup ClassLoader and the user-defined class loader. The startup class loader is a part of JVM implementation, and the user-defined class loader is a part of Java program, which must be a subclass of the classloader class. (The following situation is for SunJDK 1.2) Dynamic ClassLoader: only the installation path of the system class (the class file of JavaAPI) is searched for the user-defined class loader to be loaded. System class loader: created when the JVM is started, used to find the class to be loaded in the CLASSPATH directory. Other user-defined class loaders: It is necessary to talk about several methods of the classloader class first, and understand how they can help to understand the user-defined class loader. protectedfinalClassdefineClass(Stringname,bytedata[],intoffset,intlength) protectedfinalClassdefineClass(Stringname,bytedata[],intoffset,intlength,ProtectionDomainprotectionDomain); ProtectedDefinedclassFindSystemClass (String Name) ProtectedDefinedVoidresolveClass (Class C) is used to import binary class files (new types) into the method area. That is to say, the Class referred to here is a user-defined class (that is, it is responsible for loading classes). findSystemClass is loaded through the fully qualified name of the type, first through the system class loader or the startup class loader, and returns the class object. ResolveClass: Let the class loader connect (including verifying, allocating memory to initialize, and resolving symbolic references in types into direct references), which involves the problem of Java namespace. JVM ensures that all classes referenced by a class loaded by a class loader are loaded by this class loader, and classes loaded by the same class loader can access each other, but classes loaded by different class loaders can't see each other, thus achieving effective shielding. 2. Execution engine: it is either executing bytecode or executing local methods. To execute the engine, it has to be an instruction set. Each instruction contains a single-byte operation code followed by 0 or more operands. (1) How does the instruction set design with stack as the design center rather than register as the center meet the requirements of Java system? Platform independence: stack as the center makes it more convenient to realize Java on machines with few registers. compiler generally use Stack to transfer the intermediate results of compilation to the connection optimizer. If the instruction set is based on Stack, it is beneficial to combine the optimization work at runtime with the execution engine that performs immediate compilation or adaptive optimization. In layman's terms, it makes the data structure for compilation and operation unified and more. Network mobility: compactness of class files. Security: Most opcodes in the instruction set indicate the type of operation. (It is beneficial to improve the execution speed by using the data stream analysis period for one-time verification when loading, rather than verifying when executing each instruction). (II) Execution technology The main execution technologies are: interpretation, just-in-time compilation, adaptive optimization, and chip-level direct execution, in which interpretation belongs to the first generation JVM, just-in-time compilation JIT belongs to the second generation JVM, and adaptive optimization (currently adopted by Sun's HotspotJVM) draws on the experience of the first generation JVM and the second generation JVM, and adopts a combination of the two to adaptively optimize: at first, all codes are interpreted and executed, and then the code execution is monitored. If the method is no longer used frequently, the compiled code will be cancelled and it will still be interpreted and executed. 3. Runtime data area: mainly including: method area, heap, Java stack, PC register, local method stack (1) method area and heap shared by all threads: method area for storing objects created by all programs at runtime: when the JVM's class loader loads the. class file and parses it, the parsed type information is put into the method area. (2)Java stack and PC register are exclusively owned by threads. During the creation time of new threads, (3) Local method stack: storing the state of local method calls. The main contents of the runtime data area are generally introduced above, and are described in detail below. If you want to introduce the data area, you have to explain the data type in JVM. Data types in JVM: The basic data unit in JVM is word, and the length of word is determined by the specific implementer of JVM. The data types include basic types and reference types. (1) The basic types include: numeric types (including all basic Java data types except boolean), boolean (using int in JVM to represent, 0 means false, and other int values mean true) and Return Address (internal type of JVM) (2) Reference types include: array type, class type, and interface type. The presentation of data in JVM is described in front. Let's look at the method area in the data area input to JVM first: As mentioned above, the method area is mainly used to store the type information extracted from the class file by JVM. How is the type information stored? As we all know, Java uses big endian (big? Endian: that is, low-byte data is stored in high-level memory. If 1234, 12 is high-level data and 34 is low-level data, the storage format in Java should be 12 with low address of memory, 34 with high address of memory, and the storage format in x86 is the opposite) to store data, which is actually a class file. Type information: including the fully qualified name of the Class, the direct parent Class of the class, the class type or the interface type, the modifier of the class (public, etc.), the list of all direct parent interfaces, and the class object provides a window to access this information (available through Class.forName ("") or instance.getClass ()). The following is the method of the class. I believe everyone will suddenly realize that (I see) getname (), getsuperclass (), isinterface (), getinterfaces (), getclassloader (); As part of the type information, the static variable holds the reference to the ClassLoader Class: when dynamically connecting, it loads the references of other classes referenced in this class to the class class: inevitably, the constant pool of this type has been mentioned above: including direct constants (String, Integer and floatpoint constants) and symbolic references to other types, fields and methods (note: the constant pool here is not a place to store constants in the ordinary sense, and these symbolic references may be variables that we come into contact with in programming). Because of these symbolic references, This makes the constant pool a vital part of the field information in the dynamic connection of Java programs: the field method information declared in the general sense; the information of each method in the type; the compilation constant: refers to the class variable class declared by final or initialized with a known value at compile time to copy all the constants into its constant pool or its byte stream. Method table: an array, including all the direct references of instance methods that its instances may call (including those inherited from the parent class). In addition, if a class is not abstract and local, the bytecode, operand stack, stack frame and exception table of the method should be saved. Example: classLava{ privateintspeed=5; voidflow(){} classVolcano{ publicstaticvoidmain(String[]args){ Lavalava=newLava(); lava.flow(); }} Run the command JavaVolcano; (1)JVM finds Volcano.class and pours it in, and extracts the corresponding type information to the method area. By executing the bytecode in the method area, the JVM executes the main () method, (the pointer to the constant pool of Vocano class will always be saved during execution) (2) The first instruction in the main () tells the JVM to allocate memory for the class listed in the first item of the constant pool (here again, it is explained that the constant pool does not only store constant information), and then the JVM finds the first item of the constant pool and finds that it is a symbolic reference to the Lava class, so it checks the method area. See if the Lava class is loaded, and the result is that it is not loaded, then look up "Lava.class", write the type information into the method area, and replace the symbol reference in the original constant pool of Volcano with a pointer to the Lava class information in the method area, that is, replace the symbol reference with a direct reference. (3) The 3)JVM sees the new keyword and prepares to allocate memory for lava. According to the first item in the constant pool of Volcano, it finds the location of lava in the method area, and analyzes how many pairs of space are needed. After confirmation, it allocates space on the heap, and initially sets the speed variable to 0, and pushes the reference of Lava object into the stack. (4) Call Lava's flow () method, and after roughly understanding the content of the method area, Let's take a look at the heap implementation of heap Java objects: Java objects are mainly composed of instance variables (including those declared by its own class and its parent class), pointers to class data in the method area, pointers to method tables, object locks (unnecessary), waiting collections (unnecessary), and GC-related data (unnecessary) (mainly depending on the GC algorithm, for example, for the mark-and-clear algorithm, it is necessary to mark whether the object is referenced and whether Fin has been called). So why do Java objects have pointers to class data? We consider it from several aspects: first, when converting an object reference to another type in a program, how to check whether the conversion is allowed? Class data is needed. Secondly, when dynamically binding, you don't need reference types, but runtime types. The puzzle here is: Why do you save actual types instead of reference types in class data? I'll leave this question for now. I think I can understand the pointer to the method table in the following reading notes: it is similar to VTBL in C++, which is beneficial to improve the efficiency of method calling: object lock: it is used to realize mutually exclusive access to data shared by multiple threads; it is used to make multiple threads coordinate their merits and demerits in order to achieve the same goal. (Note the wait(),notify(),notifyAll () methods in the Object class). Heap implementation of Java arrays: Arrays also have a Class instance associated with their classes, and arrays with the same dimension and type are instances of the same class. Representation of array class names: such as [[LJava/lang/Object means Object[][], [I means int[], [[b means byte[][][] [] So far, the heap has been roughly introduced. Let's introduce the program counter and Java stack program counter: it is unique to each thread and is created when the thread starts. If the thread executes the Java method, the PC saves the next one. If thread executes the native method, the value of Pc is undefinedJava stack: Java stack saves the running state of thread in units of frames, and Java stack has only two operations, namely, pushing and popping frames. Each frame represents a method, and Java methods have two ways of returning, return and throwing exceptions, both of which will cause the corresponding frame of the method to be popped and release memory. Frame composition: local variable area (including method parameters and local variables, for instance method, this type should be saved first, in which method parameters are strictly placed in the declaration order, and local variables can be arbitrarily placed), operand stack, and frame data area (used to help support the resolution of constant pool, normal method return and exception handling). Local method stack: it depends on the implementation of local methods. For example, if a local method implemented by a JVM uses the C connection model as an excuse, the local method stack is the C stack. It can be said that when a thread calls a local method, it enters a field that is not limited by the JVM, that is, the JVM can dynamically extend itself by using the local method. I believe everyone understands what JVM is. Original link: blogs.com/chenzhao/archive/2011/08/14/2137713.html.