JVM understanding

1. JVM Architecture

   

1. Program counter

      Each thread has a program calculator, which is a pointer to the method bytecode (the next instruction code to be executed) in the method area. The execution engine reads the next instruction, which is a very small memory space. It can almost be ignored.

2. Virtual machine stack

   ① What is a stack
         The stack is also called stack memory. It is in charge of the running of the Java program. It is created when the thread is created. Its life cycle follows the life cycle of the thread. When the thread ends, the stack memory is released. There is no garbage collection problem for the stack. As long as the thread As soon as the stack is over, it is Over, and the life cycle is the same as that of the thread, which is private to the thread.
          Both primitive type variables and object reference variables are allocated in the stack memory of the function.
     ② What does the stack store?
     There are mainly three types of data stored in the stack frame:
          Local Variables: Input parameters and output parameters and variables within methods;
          Operand Stack: Record the operations of popping and pushing into the stack;
          Stack frame data (Frame Data): including class files, methods, etc.
     ③ stack operation principle
     The data in the stack exists in the format of a stack frame. The stack frame is a memory block, a data set, and a data set related to methods and runtime data. When a method A is called, it is A stack frame F1 is generated and pushed into the stack, and the A method calls the B method, so the generated stack frame F2 is also pushed into the stack, and the B method calls the C method, so the generated stack frame F3 is also pushed. Push into the stack... After the execution is completed in sequence, first pop and then enter... F3 stack frame, then F2 stack frame, and then F1 stack frame.
     Follow the "first in, last out"/"last in first out" principle.
 
     Stores primitive types and references.

3. Local method stack

4. Heap

     The heap area is the largest in the JVM. The objects and data of the application are stored in this area. This area is also shared by threads and is also the main recycling area of ​​the gc. There is only one heap class for a JVM instance. The size of the heap memory is adjustable. 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:

  ① Freshmen area
       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 and Survivor pace. All classes are new in the Eden area. There are two survival areas: area 0 (Survivor 0 space) and area 1 (Survivor 1 space). When the space in the Garden of Eden is used up, the program needs to create objects again, and the garbage collector of the JVM will perform garbage collection (Minor GC) on the Garden of Eden, and move the remaining objects in the Garden of Eden to the surviving area 0. If the surviving area 0 is also full, then the area is garbage collected, and then moved to area 1. What if 1 go is full? Then move to the retirement area. If the retirement area is also full, then a Major GC (FullGCC) will be generated at this time to clean up the memory of the retirement area. If the old-age area still cannot save the object after performing Full GC, an OOM exception "OutOfMemoryError" will be generated.
     If java.lang.OutOfMemoryError: Java heap space exception occurs, it means that the heap memory of the Java virtual machine is insufficient. There are two reasons:
    a. The heap memory setting of the Java virtual machine is not enough, which can be adjusted by the parameters -Xms and -Xmx.
     b. A large number of large objects are created in the code and cannot be collected by the garbage collector for a long time (there are references).
     ② Pension area
         The retirement area is used to save the JAVA objects screened out from the new area. Generally, pool objects are active in this area.
     ③ Permanent area
         The permanent storage area is a resident memory area 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, and the data loaded into this area will not be used. The memory occupied by this area will be released only when the JVM is closed after being collected by the garbage collector.
     If java.lang.OutOfMemoryError: PermGen space appears, it means that the Java virtual machine does not set enough permanent generation Perm memory. There are two reasons:
     a. Program startup needs to load a large number of third-party jar packages. For example: too many applications are deployed under one Tomcat.
     b. A large number of classes generated by dynamic reflection are continuously loaded, eventually causing the Perm area to be full.
     illustrate:
     Jdk1.6 and before: the constant pool is allocated in the permanent generation.
     Jdk1.7: Yes, but it has been gradually "de-permanent generation".
     Jdk1.8 and later: None (java.lang.OutOfMemoryError: PermGen space, this kind of error will not appear in JDK1.8).

5. Method area

  The method area is shared by all threads, all fields and method bytecodes, and some special methods such as constructors, and the interface code is also defined here. Simply put, the information of all defined methods is stored in this area, which belongs to the shared interval.
         Static variables + constants + class information + runtime constant pools are stored in the method area, and instance variables are stored in heap memory.

 2. Garbage Collection Algorithm

      1. Mark-clean method

      2. Copy method

      3. Marking-Organizing Method

      4. Generation method: new generation replication

                         Old age: mark-clean or mark-clean

Guess you like

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