Java JVM virtual machine knowledge points

1. JVM virtual machine diagram:




    It is the same for us who are engaged in development. Now there are more and more popular frameworks, and the encapsulation is more and more perfect. Various frameworks can handle everything, and there is almost no need to pay attention to the underlying implementation. Junior programmers As long as you are familiar with the basic usage, you can quickly develop and go online; but for advanced programmers, the cultivation of internal skills is more and more important, such as algorithms, design patterns, underlying principles, etc. Only after these basics are proficient, can they develop During the process, we can know why, and when a problem occurs, we can quickly locate the essence of the problem.
    For Java programmers, the Spring Family Bucket can handle almost everything, the Spring Family Bucket is a subtle move, and the JVM is a very important part of the internal skills. There are performance problems online, and JVM tuning is an unavoidable problem. Therefore, the importance of JVM basic knowledge to advanced programmers is unnecessary.
    1. The JVM system is divided into four major parts:
        1. Class loading mechanism
        2. JVM memory structure
        3. GC algorithm garbage collection
        4. GC analysis command tuning
    2. Class loading mechanism
        1. What is class loading
        2. Class life cycle
        3. Class loader
        4. Parent delegation model
    3. What is class loading
        Class loading refers to reading the binary data in the .class file of the class into memory, placing it in the method area of ​​the runtime data area, and then creating a java.lang.Class object in the heap area for Encapsulates the data structure of the class within the method area. The final product of class loading is the Class object located in the heap area. The Class object encapsulates the data structure of the class in the method area and provides Java programmers with an interface to access the data structure in the method area.
    Four. The life cycle of the class
        1. Load, find and load the binary data of the class, and also create an object of the java.lang.Class class in the Java heap.
        2. Connection, the connection contains three parts: verification, preparation, and initialization. 1) Verification, file format, metadata, bytecode, symbolic reference verification; 2) Prepare, allocate memory for static variables of the class and initialize them to default values; 3) Parse, convert symbolic references in the class to Direct reference
        3. Initialize, assign the correct initial value to the static variable of the class
        4. Use, use in the new object program     5.         Unload
        , perform garbage collection A class library stored in JDK\jre\lib (JDK represents the installation directory of JDK, the same below), or in the path specified by the -Xbootclasspath parameter, and can be recognized by the virtual machine         2. Extension class loader: Extension ClassLoader, The loader is implemented by sun.misc.Launcher$ExtClassLoader, which is responsible for loading all class libraries in the DK\jre\lib\ext directory, or in the path specified by the java.ext.dirs system variable (such as javax.* class), developers can directly use the extension class loader.



        3. Application class loader: Application ClassLoader, which is implemented by sun.misc.Launcher$AppClassLoader, which is responsible for loading the classes specified by the user class path (ClassPath), and developers can use this class loader directly
    . .Class loading mechanism
        1. Overall responsibility, when a class loader is responsible for loading a class, other classes that the class depends on and references will also be loaded by the class loader, unless another class loader is displayed to be used. Loading
        2. Parent class delegation, let the parent class loader try to load the class first, and only try to load the class from its own classpath when the parent class loader cannot load the class
        3. Cache mechanism, the cache mechanism will It is guaranteed that all loaded classes will be cached. When a class needs to be used in the program, the class loader will first look for the class from the cache area. Only if the cache area does not exist, the system will read the binary data corresponding to the class, and Convert it into a Class object and store it in the buffer. This is why after modifying the Class, the JVM must be restarted, and the modification of the program will take effect.
    Seven. JVM memory structure
        1. The method area and the pair are the memory areas shared by all threads; while the java stack, local method stack and programmer counter are running is a thread-private memory area.
        2. The Java heap (Heap) is the largest piece of memory managed by the Java virtual machine. The Java heap is a memory area shared by all threads, created when the virtual machine starts. The only purpose of this memory area is to store object instances, and almost all object instances allocate memory here.
        3. Method Area, Method Area, like the Java heap, is a memory area shared by each thread, which is used to store class information, constants, static variables, and just-in-time compilers that have been loaded by the virtual machine. After the code and other data.
        4. The program counter (Program Counter Register), the program counter (Program Counter Register) is a small memory space, its function can be regarded as the line number indicator of the bytecode executed by the current thread.
        5. JVM stacks (JVM Stacks), like program counters, Java Virtual Machine Stacks (Java Virtual Machine Stacks) are also private to threads, and their life cycle is the same as threads. The virtual machine stack describes the memory model of Java method execution: when each method is executed, a stack frame is created at the same time to store the local variable table, operation stack, dynamic link, method exit and other information. The process of each method being called until the execution is completed corresponds to the process of a stack frame from being pushed to the stack in the virtual machine stack.
        6. Native Method Stacks (Native Method Stacks), Native Method Stacks (Native Method Stacks) and the virtual machine stack play a very similar role, the difference is that the virtual machine stack executes Java methods (that is, bytes) for the virtual machine. code) service, and the local method stack is for the Native method used by the virtual machine.
    8. Object allocation rules
        1. Objects are preferentially allocated in the Eden area. If there is not enough space in the Eden area, the virtual machine performs a Minor GC.
        2. Large objects directly enter the old age (large objects refer to objects that require a large amount of contiguous memory space). The purpose of this is to avoid a large number of memory copies between the Eden area and the two Survivor areas (the new generation uses a copy algorithm to collect memory).
        3. Long-lived objects enter the old age. The virtual machine defines an age counter for each object. If the object has undergone one Minor GC, the object will enter the Survivor area. After each Minor GC, the age of the object will be incremented by 1, until the object reaches the threshold and enters the elderly area.
        4. Dynamically determine the age of the object. If the sum of the sizes of all objects of the same age in the Survivor area is greater than half of the Survivor space, objects whose age is greater than or equal to this age can directly enter the old age.
        5. Space allocation guarantee. Every time the Minor GC is performed, the JVM will calculate the average size of the objects moved from the Survivor area to the old area. If this value is greater than the remaining value of the old area, a Full GC will be performed. If it is smaller than the HandlePromotionFailure setting, only Monitor will be performed. GC, if false, full GC is performed.
    Nine. GC
    algorithm There are three basic GC algorithms: mark-sweep algorithm, copy algorithm, mark-compression algorithm, our commonly used garbage collectors generally use generational collection algorithm.
        1. Mark-sweep algorithm, "Mark-Sweep" (Mark-Sweep) algorithm, as its name, the algorithm is divided into "marking" and "sweeping" two stages: first mark all the objects that need to be recycled, after marking After completion, all marked objects are uniformly recycled.
        2. Copying algorithm, the "Copying" collection algorithm, which divides the available memory into two equal-sized blocks according to their capacity, and only uses one of them at a time. When the memory of this block is used up, the surviving objects are copied to another block, and then the used memory space is cleaned up once.
        3. Mark-compression algorithm, the marking process is still the same as the "mark-clean" algorithm, but the subsequent steps are not to clean up the recyclable objects directly, but to move all surviving objects to one end, and then directly clean up beyond the end boundary of memory
        4. The generational collection algorithm, the "Generational Collection" algorithm, divides the Java heap into the new generation and the old generation, so that the most appropriate collection algorithm can be adopted according to the characteristics of each generation.
    10. Garbage collector
        1.Serial collector, the serial collector is the oldest, most stable and efficient collector, it may cause longer pauses and only use one thread to recycle.
        2.ParNew collector, ParNew collector is actually a multi-threaded version of Serial collector.
        3. Parallel collector, Parallel Scavenge collector is similar to ParNew collector, Parallel collector pays more attention to the throughput of the system.         4.Parallel
        Old collector, Parallel Old is the old version of the Parallel Scavenge collector, using multi-threading and "mark-sort" algorithm
Pause-time-targeted collectors.
        6. G1 collector, G1 (Garbage-First) is a server-oriented garbage collector, mainly for machines equipped with multiple processors and large-capacity memory. While meeting the GC pause time requirements with a high probability, it also has the High-throughput performance characteristics
        7. GC algorithm and garbage collector algorithm diagram and more detailed content refer to JVM (3): Java GC algorithm garbage collector
    11. GC log analysis
        extracts part of the GC log (the first part is the young generation gc recycling; The latter part is full gc recovery):
        2016-07-05T10:43:18.093+0800: 25.395: [GC [PSYoungGen: 274931K->10738K(274944K)] 371093K->147186K(450048K), 0.0668480 secs] [Times: user=0.17 sys=0 0.07 secs]
        2016-07-05T10:43:18.160+0800: 25.462: [Full GC [PSYoungGen: 10738K->0K(274944K)] [ParOldGen: 136447K->143379K(302592K)] 147186K->140379K(57756K) PSPermGen: 85411K->85376K(171008K)], 0.6763541 secs] [Times: user=1.75 sys=0.02, real=0.68 secs]
        According to the above log analysis, PSYoungGen, ParOldGen, and PSPermGen belong to the Parallel collector. Among them, PSYoungGen represents the memory change of the young generation before and after gc reclamation; ParOldGen represents the memory change of the old generation before and after gc reclamation; PSPermGen represents the memory change of the permanent area before and after gc reclamation. Young gc mainly recycles memory frequently for the young generation and takes a short time; full gc will return the entire heap memory, which takes a long time, so generally try to reduce the number of full gc.
    Twelve. Tuning commands
    Sun JDK monitoring and failure The processing commands are jps jstat jmap jhat jstack jinfo
        1.jps, JVM Process Status Tool,
        2.jstat, JVM statistics Monitoring is a command used to monitor the runtime status information of the virtual machine. It can display the running data such as class loading, memory, garbage collection, and JIT compilation in the virtual machine process.
        3.jmap, JVM Memory Map command is used to generate heap dump files
        4.jhat, JVM Heap Analysis Tool command is used in conjunction with jmap to analyze the dump generated by jmap, jhat has a built-in mini HTTP/HTML server to generate dump After the analysis result of , you can view 5.jstack in the browser, which is
        used to generate the thread snapshot of the current moment of the java virtual machine.
        6.jinfo, JVM Configuration info This command is used to view and adjust the virtual machine running parameters in real time.
        For detailed command usage, please refer to JVM (4): JVM Tuning - Commands Chapter
    13. Tuning tools
    commonly used tuning tools are divided into two categories. JDK comes with monitoring tools: jconsole and jvisualvm, and third parties: MAT (Memory Analyzer) Tool), GChisto.
        1.jconsole, Java Monitoring and Management Console is the java monitoring and management console that comes with JDK starting from java5, which is used to monitor memory, threads and classes in the JVM.
        2.jvisualvm, jdk comes with all-round tools, It can analyze memory snapshots and thread snapshots; monitor memory changes, GC changes, etc.
        3.MAT, Memory Analyzer Tool, an Eclipse-based memory analysis tool, is a fast, feature-rich Java heap analysis tool, which can help us find memory leaks and reduce memory consumption
        4.GCisto, a professional analysis tool for gc logs tool

Guess you like

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