is a structure diagram of a Java Virtual machine:
each Java Virtual machine has a class mount subsystem that mounts the type (class or interface) according to the given fully qualified name. Similarly, each Java virtual machine has an execution engine that is responsible for executing instructions that are contained in the methods of the loaded class. When a Java Virtual machine runs a program, it needs memory to store a lot of information, and the Java Virtual Machine organizes the information into several " runtime data areas " for ease of administration. The runtime data area consists of five parts ( method area, Java heap area, Java stack area, program counter, local method stack ). 1. Class loading SubsystemIn a Java virtual machine, the part that is responsible for finding and loading the type is called the class loading subsystem. The class loader subsystem implements the entire process of class loading in a Java Virtual machine, which must be performed in the following order:(1) loading--The main completion of 3 things:1. Obtain a binary byte stream that defines this class by using the fully qualified name of a class. 2. Convert the static storage structure represented by this byte stream into the run-time data structure of the method area. 3. Generate a Java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area. (2) connection--point to verify, prepare, and parse (optional). Verify that the information contained in the byte stream of the class file conforms to the requirements of the current virtual machine and does not compromise the security of the virtual machine itself. you are ready to allocate memory for the class variable and initialize it to the default value. parsing converts a symbol reference in a type to a direct reference. (3) Initialize-initializes the class variable to the correct initial value. ClassLoader : Used to load Java classes into a Java virtual machine to implement a code module that "obtains a binary byte stream description of this class through the fully qualified name of a class." for any class, it is necessary to establish its uniqueness in the Java Virtual machine, together with the class loader that loads it, and the class itself, each with a separate class namespace. That is, comparing whether two classes are "equal" makes sense only if the two classes are loaded by the same classloader. Java virtual machines have two kinds of loaders: Start the ClassLoader and the user-defined class loader. The former is implemented by C + + and is part of the Java Virtual Machine implementation, while the latter is implemented by a Java program, independent of the virtual machine outside. 2. Run-time data area2.1 Method Area(thread sharing)used to store the class information that has been loaded by the virtual machine, solid, static variables, timely compiled code and other data. The Java Virtual machine specification describes the method area as a logical part of the heap, but in order to differentiate it from the Java heap, it is called the alias: Non-heap (not a heap). for each mounted type, the virtual machine stores the following types of information in the method area:the fully qualified name of this typeThe fully qualified name of this type of direct superclass (unless the type is java.lang.Object, it has no superclass)Whether this type is a class type or an interface typeThis type of access modifier (a subset of public, abstract, or final)an ordered list of fully qualified names for any direct hyper-interface In addition to the basic type information listed above, the virtual machine has to store the following information for each mounted type:constant pool of this typeField Informationmethod Informationall Class (static) variables except constantsa reference to the class ClassLoadera reference to class2.1.1 Constant Pool holds string constants and Basic type constants (public static final) and symbol references . The virtual machine must maintain a constant pool for each type of load. A constant pool is an ordered set of constants used by the type, including direct constants and symbolic references to other types, fields, and methods, and the data items in the pool are accessed by the index as if they were arrays. Because a constant pool stores symbolic references to all types, fields, and methods used by the corresponding type, it plays a central role in the dynamic connection of Java programs. 2.1.2 Field Informationfor each field declared in a type. The following information must be saved in the method area. In addition, the order in which these fields are declared in the class or interface must also be saved. 0 Field names0 type of field0 modifier for field (public, private, protected, static, final, volatile, a subset of transient)2.1.3 Method Informationfor each method declared in a type, the following information must be saved in the method area. As with fields, the order in which these methods are declared in a class or interface must also be saved. 0 Method Name0 return type (or void) of the method0 Number and type of method parameters (in declaration order)0 Modifier for method (public, private, protected, static, final, synchronized, native, a subset of abstract) In addition to the entries listed in the preceding list, if a method is not abstract and local, it must also hold the following information: 0 byte code of the Method (Bytecodes)0 The size of the local variable area in the stack frame of the operand stack and the method0 Exception Table2.1. Class 4 (Static) variableA class variable is shared by all instances of the class, but it can be accessed even if there are no class instances. These variables are only related to classes--not instances of classes, so they are always stored as part of the type information in the method area. In addition to the compile-time constants declared in the class, the virtual machine must allocate space for these class variables in the method area before using a class. The compile-time constant, which is the class variable initialized with the final declaration and the value that is known at compile times, differs from the usual class-variable processing, and each type that uses a compile constant will copy all its constants into its own constant pool, or embed it in its byte stream. As part of a constant pool or byte stream, the compile-time constant is stored in the method area-just like a generic class variable. However, when generic class variables are saved as part of the data surface of the type that declares them, compile-time constants are saved as part of the type that uses them. 2.1.5 Reference to ClassLoader class when each type is loaded, the virtual machine must track whether it is loaded by the startup class loader or by the user-defined class loader. If the user-defined class loader is loaded, the virtual machine must store a reference to the loader in the type information. This is saved as part of the type data in the method table. the virtual opportunity uses this information during a dynamic connection. When a type references another type, the virtual opportunity request loads the class loader that initiates the reference type to mount the referenced type. This dynamic connection process is also critical to how the virtual machine separates namespaces. To be able to properly perform dynamic connections and maintain multiple namespaces, the virtual machine needs to know in the method table which class loader each class is loaded in. 2.1.6 reference to class class for each loaded type (whether it is a class or an interface), the virtual machine creates an instance of the Java.lang.Class class accordingly, and the virtual machine must somehow associate the instance with the type data stored in the method area. The method Area Use example is detailed in the reference one 2.2Java heap (thread sharing)All Class instance objects (including string objects) or arrays that are created at run time by a Java program are placed in the same heap. There is only one heap space in a Java virtual machine instance, so all threads will share the heap. And because a Java program has a single Java virtual machine instance, each Java program has its own heap space--they don't interfere with each other. However, multiple threads of the same Java program share the same heap space, in which case the synchronization of multi-threaded access Objects (heap data) is considered. The Java heap is the primary area of garbage collector management, and the Java Virtual machine has an instruction to allocate new objects in the heap without releasing the memory instructions, just as you cannot explicitly release an object in the Java code area. The virtual machine itself is responsible for deciding how and when to release memory that is occupied by objects that are no longer referenced by the program being run. Typically, the virtual machine gives this task to the garbage collector. ↑ about Java heap and Java stack storage simple memory : All new objects, there is a heap area, all references exist in the stack area ★ ↓ 2.3Java Stack (thread-private) stores the values of the base type , references to objects (including string references, array references), and ReturnAddress, which are allocated during compilation. each time a new thread is started, the Java Virtual machine assigns it a Java stack. The Java stack saves the running state of a thread in frames. The virtual machine only performs two operations directly on the Java stack: the stack and the stack in frames. The method that a thread is executing is called the current method of the thread, the stack frame used by the current method is called the current frame, the class to which the current method belongs is called the current class, and the current class's Chang is called the current constant pool. When a thread executes a method, it keeps track of the current class and the current constant pool. In addition, when the virtual opportunity is inside the stack operation instruction, it performs operations on the data in the current frame. each time a thread invokes a Java method, the virtual machine presses a new frame into the Java stack of the thread. This new frame naturally becomes the current frame. When executing this method, it uses this frame to store data such as parameters, local variables, intermediate operation results, and so on. Java methods can be done in two ways. A return returned by return, known as a normal return, which is terminated abnormally by throwing an exception. Either way, the virtual machine pops the current frame out of the Java stack and frees it so that the frame of the previous method becomes the current frame. all data on the Java frame is private to this thread. No thread can access the stack data of another thread, so we do not need to consider the access synchronization of stack data in multithreaded situations. When a thread calls a method, the local variables of the method are saved in the frame of the calling thread's Java stack. Only one line Cheng Nen always accesses those local variables, that is, the thread that invokes the method. the memory snapshot of the stack frame when the two local variables are added is as follows: 2.4 Program counters (thread-private)for a running Java program, each of these threads has its own PC (program counter) register, which is used to indicate the address of the next instruction. Basic functions such as branching, looping, jumping, exception handling, thread recovery, and so on, need to rely on this counter to complete. It is created at the start of the thread, and the size of the PC register is a word length (so it can hold a local pointer and can hold a returnaddress). When a thread executes a Java method, the contents of the PC register are always the "address" of the next instruction to be executed, where the "address" can be a local pointer or an offset in the method bytecode relative to the method's start instruction. If the thread is executing a local method, the value of the PC register at this time is "undefined". 2.5 Local Method stack (thread private)This is where Java calls the operating system's local library, which differs from the Java stack in that the Java virtual machine executes the Java Method (bytecode) service for the virtual machine, while the local method stack performs the native method service used by the virtual machine. When a thread calls a local method, it enters a completely new world that is no longer restricted by the virtual machine. The language, usage, and data structures used in the local method stack in the virtual machine specification are not mandatory, so a specific virtual machine is free to implement it. any local method interface will use some kind of local method stack. When a thread calls a Java method, the virtual opportunity creates a new stack frame and presses it into the Java stack. However, when it calls the local method, the virtual machine keeps the Java stack intact, and no longer presses the new frame into the thread's Java stack, and the VM simply dynamically connects and invokes the specified local method directly. If a virtual machine implements a local method interface that uses the C connection model, then its local method stack is the C stack. When a C program calls a C function, its stack operation is deterministic. The arguments passed to the function are pressed into the stack in a certain order, and its return value is passed back to the caller in a deterministic manner. Again, this is the behavior of the local method stack in the virtual machine implementation. It is likely that the local method interface needs to callback the Java method in the Java virtual machine, in which case the thread will save the state of the local method stack and go to another Java stack. depicts a scenario where a local method calls back another Java method in a virtual machine when a thread invokes a local method. This image shows a panorama of the threads running inside the Java Virtual machine. A thread may execute a Java method throughout its lifecycle, manipulate its Java stack, or it may jump between the Java stack and the local method stack without hindrance. the thread first calls two Java methods, and the second Java method calls a local method, which causes the virtual machine to use a local method stack. Suppose this is a C language stack in which there are two C functions, the first C function is called by the second Java method as a local method, and the C function calls the second C function. The second C function then callbacks a Java method (the third Java method) through the local method interface, and finally the Java method calls a Java method (which becomes the current method in the diagram). 3. Execution Enginethe core of the Java Virtual machine, which controls loading Java bytecode and parsing; for a running Java program, each thread is an instance of a separate virtual machine execution engine, from the beginning of the thread's life cycle to the end, either executing the bytecode or executing the local method. 4. Local interfaceThe local method stack and the operating system library are connected. Reference: Http://www.cnblogs.com/java-my-life/archive/2012/08/01/2615221.htmlHttp://www.codeceo.com/article/java-jvm-struct-learn.html#0-tsina-1-96459-397232819ff9a47a7b7e80a40613cfe1 deep understanding of Java Virtual Machine ——— JVM advanced features and best practices
Java Virtual Machine Architecture analysis