JVM knowledge point learning

JVM knowledge learning

1. Location of JVM (in jre)

Run on the operating system (window, Linux, Mac...), the operating system runs on the hardware system.

2. JVM Architecture JVM architecture

note:

  1. Stack, the program counter cannot be garbage collected
  2. Most of JVM tuning is tuning the heap

3. Class Loader

[External link image transfer failed, the source site may have an anti-leech link mechanism, it is recommended to save the image and upload it directly (img-XVEfvfj4-1598964975996)(./类loader.png)]

  1. Role: Load class file
    • Loader that comes with the virtual machine
    • BootstrapClassLoader BootstrapClassLoader
    • ExtClassLoader
    • Application Loader AppClassLoader

4. Parental delegation mechanism (security)

  1. The class loader receives a request for class loading
  2. Delegate this request upwards to the parent class loader to complete, and delegate upwards until class loading is started;
  3. The startup loader checks whether the current class can be loaded, and it ends when it can load, and uses the current class loader, otherwise an exception is thrown and the sub-loader is notified to load;
  4. Repeat step 3.

5.native

  1. Anything with the native keyword indicates that the scope of java is not reached, and the underlying C language library will be called. Enter the native method stack ==> call the native method interface (Java Native Interface)
  2. The role of JNI: Extend the use of java and integrate different programming languages ​​for java.
  3. The memory area is specially opened up for a marked area: Native Method Stack Register native methods.
  4. In the final execution, use JNI to load the local method library.

6. PC register (program counter) Program Counter Register

Each thread has a program counter, which is private to the thread. It is a pointer to the bytecode of the method area. When the execution engine reads the next instruction, it is a very small memory space, which can almost be ignored.

7. Method Area

The method area is shared by all threads, and this area belongs to the shared area.
Static variables, constants, class information (construction methods, interface definitions), and runtime constant pools are stored in the method area, but instance variables are stored in the heap memory and have nothing to do with the method area.

8. Stack

  1. Stack: First in last out, last in first out.
    Queue: First Input First Output (FIFO: First Input First Output)
  2. The stack memory is responsible for the operation of the program, and the life cycle is synchronized with the thread; when the thread ends, the stack memory is released. For the stack, there is no garbage collection
  3. Each thread contains a stack area that retains basic data types, object references, and instance methods.
  4. Push and pop in stack frame unit.

Method stack

9. Heap

heap

  1. Heap, a JVM has only one heap memory, and the size of the heap memory can be adjusted.
  2. After class loading reads the class file, it generally stores the classes, methods, constants, and variables in the heap memory. Save the real object of the type we are referencing.
  3. Newborn area: the place where the species is born and grows, and even dies.
  4. Eden Park: All objects are new from the Eden Park.
  5. Permanent storage area: This area is resident in the memory, used to store the Class objects carried by the JDK itself, Interface metadata, and stores some environment and constants of the Java runtime. There is no garbage collection in this area! Closing the JVM virtual machine will release the memory in this area.
  6. A startup class loads a large number of third-party jar packages, Tomcat deploys too many applications, a large number of generated reflection classes, and constantly dynamically generated reflection classes are continuously loaded until the memory is full, and the OOM will be released.
    Class loading process

10. Use JProfile tool to analyze OOM reasons

  1. Can see the wrong line of the code: memory snapshot analysis tool, MAT, JProfiler
  2. MAT, JProfile function:
    • Analyze Dump memory files to quickly locate memory leaks
    • Get the data in the heap
    • Get big objects
  3. JVM parameters
    • -Xms: Set the initial allocated memory size to approximately 1/64 of the total memory
    • -Xmx: Set the maximum allocated memory to approximately 1/4 of the total memory
    • --XX:+PrintGCDetails print GC garbage collection information
    • --XX:+HeapDumpOnOutOfMemoryError print out OOM Dump

11. Garbage collection mechanism GC

  1. GC is divided into Minor GC and Full GC. Minor GC clears Eden and from and goes to to. Next, from and to are converted. Continue to clear the Eden and from areas; Full GC collects the entire heap, including the young generation, the old generation, and the permanent generation (in JDK 1.8 and later, the permanent generation will be removed and replaced with metaspace) and other modes of collecting all parts.
  2. GC algorithm: mark removal method, mark sorting method, copy algorithm, reference counting method.
  3. Copy algorithm : Divide the memory capacity into two memory blocks of equal size, use only one at a time, when this capacity is almost used up. Copy the surviving objects to another upper area, and then clean up all the used memory space.
  • Usage scenario: The object survives in a lower memory space, such as the Eden area.
  • Advantages: Memory allocation does not need to consider the replication situation such as fragmentation.
  • Disadvantages: low space utilization; the replication algorithm needs to perform more replication operations when the object survival rate is high, and the efficiency will be lower.
  1. Mark removal algorithm : The algorithm is divided into two stages: marking and clearing: first mark the objects that need to be recycled, and collect all marked objects uniformly after marking.
  • Usage scenario: used in the old age. Because the probability of recycling in the old generation is small and infrequent can reduce memory fragmentation.
  • Disadvantages: the two processes of marking and clearing are not efficient; a large number of discrete memory fragments will be generated.
  1. Marking method : Its marking stage is the same as that in the mark removal algorithm. The finishing stage is to compress all surviving objects to one end of the memory, and finally clear all the space outside the boundary.
  2. to sum up:
    • Efficiency: Copy algorithm> Mark sorting algorithm> Mark clearing algorithm
    • Memory utilization: mark sorting algorithm = mark removal algorithm> copy algorithm
    • Memory cleanliness: copy algorithm = mark sorting method> mark clearing method

12.JMM Java Memory Model

  1. Java's concurrency uses a shared memory model
  2. Role: Cache consistency protocol, used to define the rules for data reading and writing.
  3. JMM has formulated the following rules for the use of these eight instructions:
    (1) One of read and load, store and write operations is not allowed to appear separately. Even if read must be used, load must be used, store must be written;
    (2) The thread is not allowed to discard its latest assign operation, that is, after the data of the work variable is changed, the main memory must be notified;
    (3) A thread is not allowed to have no assign The data is synchronized from the working memory back to the main memory;
    (4) A new variable must be born in the main memory, and the working memory is not allowed to directly use an uninitialized variable. That is, before the variable implements the use and store operations, it must go through the assign and load operations;
    (5) Only one thread can lock a variable at a time. After multiple locks, you must perform the same number of unlocks to unlock;
    (6) If you perform a lock operation on a variable, the value of this variable in all working memory will be cleared. Before the execution engine uses this variable, you must reload or assign the operation. Initialize the value of the variable;
    (7) If a variable is not locked, it cannot be unlocked. Also cannot unlock a variable locked by other threads;
    (8) Before unlocking a variable, this variable must be synchronized back to main memory.

Guess you like

Origin blog.csdn.net/Zmd_23/article/details/108350672