JVM Fundamentals

1、jvm

    1.1 jvm

        The full name of jvm is Java VirtualMachine (java virtual machine). It's called "virtual" because it's just an abstract computer defined by a specification. The Sun HotSpot virtual machine (program virtual machine) that we usually use is just one of the specific implementations (there are also virtual machines such as BEA JRockit, IBM J9, etc.). A virtual computer is implemented by software on an actual computer. Unlike VMWare (system virtual machine) and similar software, you can't see the jvm, it exists in memory.    

        When a Java program is started, a virtual machine instance is created. When the program closes and exits, the virtual machine instance also dies. If you run three Java programs at the same time on the same computer, you will get three Java virtual machine instances. Each Java program runs in its own instance of the Java Virtual Machine.

    1.2 JVM basic structure

                      

        The following is a brief description of each part of the figure:        

            1. Class loading subsystem: Responsible for loading Class information from the file system or network, and the loaded class information is stored in a memory space called the method area.

            2. Method area: It stores class information, runtime constant pool information, including string literals and numeric constants.

            3. The java heap: It is established when the virtual machine starts, and it is the main memory working area of ​​the java program. Almost all java object instances are stored in the java heap. Heap space is shared by all threads, which is a memory space closely related to Java applications.

            4. Direct memory: It is the memory space outside the Java heap and directly applied to the system. Usually access to direct memory will be faster than the java heap. Therefore, for performance reasons, direct memory may be considered for frequent reads and writes. Since the direct memory is outside the java heap, its size is not directly limited by the maximum heap size specified by Xmx, but the system memory is limited, and the sum of the java heap and direct memory is still limited by the maximum value that the operating system can give RAM.

            5. java stack: each java virtual machine thread has a private java stack, the java stack of a thread is created when the thread is created, the frame information is stored in the java stack, and the local variables and method parameters are stored in the java stack , and is closely related to the invocation and return of java methods.

            6. The native method stack is very similar to the java stack. The biggest difference is that the java stack is used for method calls, while the native method stack is used for native method calls. As an important extension to the java virtual machine, the java virtual machine allows java to directly Call native methods (usually written in C)

            7. The garbage collection system is an important part of the java virtual machine. The garbage collector can recycle the method area, java heap and direct memory. Among them, the java heap is the focus of the work of the garbage collector. Unlike C/C++, all object space release in java is implicit, that is, there is no function like free() or delete() in java to release the specified memory area. For garbage objects that are no longer used, the garbage collection system will work silently in the background, silently searching for, identifying and releasing garbage objects, and completes fully automated management including java heap, method area and direct memory.     

             8. The PC (Program Counter) register is also a private space for each thread. The java virtual machine creates a PC register for each java thread. At any time, a java thread is always executing a method, and the method being executed is called the current method. If the current method is not a native method, the PC register will point to the instruction currently being executed. If the current method is a native method, the value of the PC register is undefined

            9. The execution engine is one of the core components of the Java virtual machine. It is responsible for executing the bytecode of the virtual machine. In order to improve the execution efficiency, the modern virtual machine will use the just-in-time compilation technology to compile the method into machine code and then execute it.

2. java heap    

    2.1 java heap

         The Java heap is the memory space most closely related to the application, and almost all objects are stored on the heap. And the Java heap is fully automatically managed. Through the garbage collection mechanism, garbage objects are automatically cleaned up without explicit release.

        Depending on the java recycling mechanism, the java heap may have different structures. The most common configuration is to divide the entire Java heap into the young generation and the old generation (the default ratio is: the young generation occupies 1/3 of the heap space, and the old generation occupies 2/3 of the heap space). Among them, the young generation stores new objects or objects that are not very old, and the old generation stores old objects. The new generation can be divided into eden area, s0 area, and s1 area (the default ratio is: 8:1:1). The s0 area and the s1 area are also called from and to areas. They are two blocks of the same size and can be interchanged. The character's memory space.

    2.2 The basic structure of java heap

        

       The new generation is where almost all JAVA objects are born, and the memory and storage of JAVA objects are all in this place. In most cases, the object is first allocated in the eden area (including a survivor, if it is from), after a new generation recovery, if the object is still alive and can be accommodated by another survivor (here the survivor is to ), then use the copy algorithm to copy these still surviving objects to the to survivor area, then clean up the eden and from survivor areas, and add the age of these surviving objects +1. In the future, every time the object survives GC in the survivor, the Increase by 1, when the age reaches a certain value (default 15, set by setting the parameter -xx:maxtenuringThreshold), these objects will become the old age! But not necessarily, when some larger objects (need to allocate contiguous memory space) directly enter the old age.

3. java stack

    The Java stack is a thread-private memory space. If the java heap is closely related to program data, then the java stack is closely related to thread execution. The basic behavior of thread execution is function call, and the data of each function call is passed through the java stack.

The java stack has a similar meaning to the stack in the data structure. It is a first-in, last-out data structure that only supports two operations: pop and push. The main content saved in the java stack is the stack frame. Every time a function is called, a corresponding stack frame will be pushed into the java stack, and at the end of each function call, a stack frame will be popped off the java stack. As shown below: stack frame and function call. Function 1 corresponds to stack frame 1, function 2 corresponds to stack frame 2, and so on. Function 1 calls function 2, function 2 calls function 3, and function 3 calls function 4. When function 1 is called, stack frame 1 is pushed onto the stack, when function 2 is called, stack frame 2 is pushed onto the stack, and when function 3 is called When function 4 is called, stack frame 3 is pushed onto the stack, and when function 4 is called, stack frame 4 is pushed onto the stack. The frame corresponding to the currently executing function is the current frame (at the top of the stack), which saves data such as the local variables of the current function, intermediate calculation results, and so on. When the function returns, the stack frame is popped from the java stack. There are two ways to return a function in the java method area, one is the normal function return, using the return instruction, and the other is throwing an exception. Either way, the stack frame will be popped.

    A stack frame contains at least several parts of the local variable table, the operand stack and the frame data area.

    

    1. Local variable table: It is one of the important components of the stack frame. It is used to save function parameters and local variables. The variables in the local variable table are only valid in the current function call. When the function call ends, the local variable table will be destroyed as the function stack frame is popped and destroyed.

     2. Operand stack: It is mainly used to save the intermediate results in the calculation process, and it is also used as a temporary storage space for variables in the calculation process. The operand stack is also a first-in-last-out data structure, which only supports two operations: push and pop. Many Java bytecode instructions require parameter passing through the operand stack.

    3. In addition to the local variable table and operand stack, the java stack frame also needs some data to support constant pool parsing, normal method return and exception handling. Most java bytecode instructions require constant pool access, and a pointer to access the constant pool is reserved in the frame data area to facilitate program access to the constant pool. In addition, when the function returns or an exception occurs, the virtual machine must restore the caller function's stack frame and allow the caller function to continue executing. For exception handling, the virtual machine must have an exception handling table, which is convenient to find the code to handle the exception when an exception occurs. Therefore, the exception handling table is also an important part of the frame data area.

4. Method area 

    Like the heap, the method area is a memory area shared by all threads, which is used to save system class information, such as class fields, methods, and constant pools. The size of the method area determines how many classes the system can save. If the system defines too many classes, resulting in the overflow of the method area, the virtual machine will also throw a memory overflow error. 

    In JDK1.6 and JDK1.7, the method area can be understood as the permanent area (Perm). The permanent area can be specified with the parameters -XX:PermSize and -XX:MaxPermSize. By default, -XX:MaxPermSize is 64M. A large permanent area can hold more class information. If the system uses some dynamic proxies, it may generate a large number of classes at runtime. If so, you need to set a reasonable size of the permanent area to ensure that memory overflow of the permanent area does not occur.

    In JDK1.8, the permanent area has been completely removed and replaced by the metadata area. The size of the metadata area can be specified with the parameter -XX:MaxMetaspaceSize (a large metadata area can enable the system to support more classes), This is a block of direct memory off-heap. Unlike the persistent area, if you do not specify a size, by default the virtual machine will exhaust all available system memory. If an exception occurs in the metadata area, the virtual machine will also throw an exception.

5. The relationship between java heap, method area, and java stack    

    Use a simple example in java to show the relationship between the java heap, method area and java stack

package com.jvm;
public class SimpleHeap {
  private int id;
  public SimpleHeap(int id){
    this.id = id;
  }
  public void show(){
    System.out.println("My id is "+id);
  }

  public static void main(String[] args) {
    SimpleHeap s1 = new SimpleHeap(1);
    SimpleHeap s2 = new SimpleHeap(2);
    s1.show();
    s2.show();
  }
}

       

        The SimpleHeap instance itself is allocated in the heap, the information describing the SimpleHeap class is stored in the method area, and the s1 and s2 local variables in the main function are stored on the java stack and point to two instances in the heap.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325657762&siteId=291194637
JVM
JVM