Overview of JVM knowledge points-required for interviews with senior and middle-level Java engineers, and jvm Overview

Source: Internet
Author: User
Tags jconsole

Overview of JVM knowledge points-required for interviews with senior and middle-level Java engineers, and jvm Overview

The same is true for developers. There are more and more popular frameworks and more comprehensive packages. Various frameworks can handle everything, and there is almost no need to focus on the underlying implementation, as long as junior programmers are familiar with basic usage methods, they can quickly develop and launch the program. However, for senior programmers, the cultivation of internal skills becomes more and more important, such as algorithms, design patterns, and underlying principles, only by mastering these basics can you understand the nature of the problem in the development process.
For Java programmers, spring family bucket can handle almost everything. spring family bucket is a subtle trick, and jvm is an important part of the Internal Skill Method. performance problems occur online, jvm tuning is an unavoidable issue. Therefore, the importance of basic JVM knowledge to senior programmers does not need to be mentioned.
I. The jvm system is divided into four parts:
1. Class Loading Mechanism
2. jvm Memory Structure
3. GC algorithm garbage collection
4. GC analysis command Optimization
Ii. Class Loading Mechanism
1. What is class loading?
2. lifecycle of a class
3. Class Loader
4. Parental Delegation Model
3. What is class loading?
Class loading refers to the operation of the class. the binary data in the class file is read into the memory, stored in the Method Area of the runtime data area, and a java is created in the heap area. lang. class Object, used to encapsulate the data structure of the Class in 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, it also provides Java programmers with interfaces to access the data structure in the method area.
Iv. lifecycle of a class
1. Load, search for and load binary data of the Class, and create a Java. lang. Class Object in the java heap.
2. The connection contains three parts: verification, preparation, and initialization. 1) Verification, file format, metadata, bytecode, and symbol reference verification; 2) Preparation, allocate memory for static variables of the class, and initialize it as the default value; 3) resolution, converts a symbolic reference in a class to a direct reference.
3. initialize and assign the correct initial value to the static variables of the class.
4. Use, used in the new output object program
5. Uninstall and perform garbage collection.
V. class loaders
1. start the Class Loader: Bootstrap ClassLoader, responsible for loading and storing it in JDK \ jre \ lib (JDK represents the JDK installation directory, the same below), or in the path specified by the-Xbootclasspath parameter, class libraries that can be recognized by virtual machines
2. extended Class Loader: Extension ClassLoader, which is created by sun. misc. launcher $ ExtClassLoader, which is responsible for loading the DK \ jre \ lib \ ext directory, or by java. ext. all class libraries (such as javax. * class), developers can directly use the extension class loader.
3. application Class Loader: Application ClassLoader, which is composed of sun. misc. launcher $ AppClassLoader is used to load the class specified by the user ClassPath. developers can directly use this class loader.
Vi. Class Loading Mechanism
1. the Class loader is fully responsible. When a Class loader is responsible for loading a Class, the Class loader is responsible for loading other classes that the Class depends on and references, unless it is displayed that another class loader is used for loading
2. parent class delegate: First let the parent class loader try to load the class. Only when the parent class loader cannot load the class will it try to load the class from its own class path.
3. the cache mechanism ensures that all loaded classes are cached. When a Class is required in the program, the Class Loader first looks for the Class from the cache area, only when the cache area does not exist will the system read the binary data corresponding to the Class, convert it into a Class object, and store it in the cache area. This is why the modification of the program takes effect only after JVM is restarted after the Class is modified.
VII. jvm Memory Structure
1. The method area and the memory area shared by all threads; while the java stack, local method stack, and programmer counter are the private memory areas of the thread.
2. Java Heap is the largest memory managed by Java virtual machine. Java heap is a memory area shared by all threads. It is created when the VM is started. The only purpose of this memory area is to store object instances, where almost all object instances allocate memory.
3. the Method Area is the same as the Java heap. It is the memory Area shared by various threads, it is used to store data such as class information, constants, static variables, and Code Compiled by the real-time compiler loaded by virtual machines.
4. program Counter Register (Program Counter Register) is a small memory space. Its role can be seen as the row number indicator of the bytecode executed by the current thread.
5. The JVM stack (JVM Stacks) is the same as the program counter. the Java Virtual Machine stack (Java Virtual Machine Stacks) is also thread-proprietary and has the same lifecycle as the thread. The Virtual Machine Stack describes the Memory Model of Java method execution: A Stack Frame is created simultaneously when each method is executed) stores information about local variable tables, Operation stacks, dynamic links, and method exits. The process of calling each method until execution is complete corresponds to the process of a stack frame in the VM Stack from the inbound stack to the outbound stack.
6. native Method Stacks and Native Method Stacks are very similar to virtual machine Stacks, the difference is that the virtual machine stack executes the Java method (also known as bytecode) service for the virtual machine, while the local method stack serves the Native method used by the virtual machine.
8. Object allocation rules
1. objects are preferentially distributed in the Eden area. If there is not enough space in the Eden area, the virtual machine executes Minor GC once.
2. large objects directly enter the old age (large objects refer to objects that require a large amount of continuous memory space ). The purpose of this operation is to avoid a large number of memory copies between the Eden zone and the two vor zones (the new generation uses the replication 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 passes through the Minor GC once, the object enters the same vor area. After that, every time the object passes through the Minor GC, the age of the object increases by 1, know that the target reaches the threshold and enters the elderly area.
4. dynamically determine the age of the object. If the total size of all objects of the same age in the region VOR is greater than half of the region vor space, objects of the same age or age can directly enter the old age.
5. space allocation guarantee. Every time Minor GC is performed, JVM calculates the average size of objects in the same vor region to the Old-Age zone. If this value is greater than the remaining value in the Old-Age zone, Full GC is performed, if the value is smaller than the HandlePromotionFailure setting, if the value is true, only Monitor GC is performed. If the value is false, Full GC is performed.
9. GC Algorithm
There are three basic GC algorithms: the Mark-removal algorithm, the copy algorithm, and the mark-compression algorithm. Common garbage collectors generally use the generational collection algorithm.
1. the Mark-Sweep algorithm, like its name, is divided into two phases: "Mark" and "clear: first, all objects to be recycled are marked, and all marked objects are recycled after marking.
2. replication algorithm: the Copying collection algorithm, which divides the available memory into two equal-size blocks by capacity and uses only one of them at a time. When the memory of this block is used up, copy the still living objects to the other block, and then clear the used memory space.
3. mark-compression algorithm, the marking process is still the same as the "mark-clear" algorithm, but the subsequent steps do not directly clean the recyclable objects, but move all the surviving objects to one end, then, the memory outside the end boundary is cleared directly.
4. the Generational Collection algorithm, the Generational Collection algorithm, divides Java heap into the new generation and the old generation, so that the most appropriate Collection algorithm can be used according to the characteristics of each generation.
10. Garbage Collector
1. The Serial collector, which is the oldest, most stable, and highly efficient collector, may generate a long pause and use only one thread for collection.
2. The ParNew collector is actually a multi-threaded version of the Serial collector.
3. The Parallel collector and the Parallel Scavenge collector are similar to the ParNew collector. The Parallel collector focuses more on the system throughput.
4. Parallel Old collector. Parallel Old is the Old version of Parallel Scavenge collector. It uses multithreading and the "tag-sorting" algorithm.
5. CMS collector, the CMS (Concurrent Mark Sweep) collector is a collector designed to get the shortest recovery pause time.
6. g1 collector, G1 (Garbage-First) is a server-oriented Garbage collector, mainly for machines with multiple processors and large memory capacity. it meets GC pause time requirements with a high probability, and also has high throughput performance characteristics.
7. GC algorithm and garbage collector algorithm diagram and for more details, refer to JVM (3): Java GC algorithm Garbage Collector
11. GC Log Analysis
Extract part of the GC log (the front part is divided into the young generation gc collection; the rear part is divided into full gc collection ):
2016-07-05T10: 43: 18.093 + 0800: 25.395: [GC [PSYoungGen: 274931 K-> 10738 K (274944 K)] 371093 K-> 147186 K (450048 K ), 0.0668480 secs] [Times: user = 0.17 sys = 0.08, real = 0.07 secs]
2016-07-05T10: 43: 18.160 + 0800: 25.462: [Full GC [PSYoungGen: 10738 K-> 0 K (274944 K)] [ParOldGen: 136447 K-> 140379 K (302592 K)] 147186 K-> 140379 K (577536 K) [PSPermGen: 85411 K-> 85376 K (171008 K)], 0.6763541 secs] [Times: user = 1.75 sys = 0.02, real = 0.68 secs]
According to the log analysis above, PSYoungGen, ParOldGen, and PSPermGen belong to the Parallel collector. PSYoungGen indicates the memory changes of the young generation before and after gc collection; ParOldGen indicates the memory changes of the old generation before and after gc collection; PSPermGen indicates the memory changes in the permanent zone before and after gc collection. Young gc is mainly used for memory recovery of the young generation, which takes a short time. full gc will return the whole heap memory for a long time, so it is generally recommended to reduce the number of full gc times.
12. Optimization command
The Sun JDK monitoring and troubleshooting Commands include jps jstat jmap jhat jstack jinfo.
1. jps, JVM Process Status Tool, displays all HotSpot VM processes in the specified system.
2. jstat, JVM statistics Monitoring is a command used to monitor the status information of virtual machine running. It can display running data such as class loading, memory, garbage collection, JIT compilation, and so on in the virtual machine process.
3. The jmap and JVM Memory Map commands are used to generate heap dump files.
4. the jhat and JVM Heap Analysis Tool commands are used together with jmap to analyze dump generated by jmap. jhat has a built-in mini HTTP/HTML server and generates dump Analysis results, can be viewed in the browser
5. jstack is used to generate the thread snapshot of the Java VM at the current time.
6. The jinfo and JVM Configuration info commands are used to view and adjust the virtual machine running parameters in real time.
For detailed command usage, refer to here JVM (4): Jvm tuning-command
13. tuning tools
Common tuning tools include jconsole and jvisualvm. Third-party tools include MAT (Memory Analyzer Tool) and GChisto.
1. jconsole, Java Monitoring and Management Console is a java Monitoring and Management Console that comes with JDK starting from Java 5. It is used to monitor the memory, threads, and classes in JVM.
2. jvisualvm, jdk comes with all-around tools, which can analyze memory snapshots and thread snapshots, monitor memory changes, GC changes, and so on.
3. MAT, Memory Analyzer Tool, an Eclipse-based Memory analysis Tool, is a fast and functional Java heap analysis Tool that helps us find Memory leaks and reduce Memory consumption
4. GChisto, a professional gc log analysis tool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.