JVM Series (eight): JVM knowledge points overview-advanced Java Engineer Interview Prerequisites

Source: Internet
Author: User
Tags jconsole

In the lakes and rivers in order to get the peerless martial arts must both inside and outside, subtle moves and deep internal strength, the foundation of martial Arts is the inner Strength. For martial arts low (like Jiangnan seven Strange) people, moves more important, because they can not rely on the internal strength directly to hurt people, only by the moves, the edge of the blade to win, but after the master, the internal skills are even more major. A low-level man moves in the marvelous also can not beat a high-strength Person. For example, You sword Sword again strong, a blade stab over, others a hand to interrupt your sword, you also how to make sword, you beat to a martial arts high person body, that person nothing, but put you hurt, how do you fight. The same two are complementary to each other, the internal strength of the deep, the original common strokes of the Power will also multiply.

For the development of We are actually the same, now popular framework more and more, packaging is more and more perfect, a variety of frameworks can take care of everything, almost no attention to the bottom of the implementation, the novice programmer as long as familiar with the basic use of methods, you can quickly develop on-line; but for senior programmers, the practice of internal strength is more important , such as algorithms, design patterns, underlying principles, and so on, only after these basic proficiency, in the development process can know its why, when problems can quickly locate the nature of the Problem.

For Java programmers, Spring family barrels can almost take care of everything, Spring family bucket is a subtle moves, the JVM is an important piece of internal strength, the performance problems on the line, JVM tuning is an unavoidable problem. therefore, the basic knowledge of the JVM to the senior programmer is not necessary to speak, the Secretary for advanced Development in the interview, the knowledge of the JVM must also be one of the criteria for Evaluation. This article combs out all the research points that the JVM needs to focus on, based on the previously written JVM series Articles.

JVM Overall Grooming

The JVM system is generally divided into four blocks:

    • The loading mechanism of the class
    • JVM Memory Structure
    • GC Algorithm garbage Collection
    • GC Analysis Command Tuning

of course, these points of knowledge in the previous articles are described in detail, here only to do the trunk comb

Here to draw a mind map, will all the knowledge point of the display, because the diagram is larger can be clicked right-click to download the enlarged VIEW.

The loading mechanism of the class

Main points of Concern:

    • What is the load of a class
    • Life cycle of a class
    • Class Loader
    • Parental delegation Model

What is the load of a class

Class loading refers to reading the binary data in the Class's. class file into memory, placing it in the method area of the Run-time data area, and then creating a Java.lang.Class object in the heap that encapsulates the data structure of the class within the method Area. The final product loaded by the class is a class object located in the heap, which encapsulates the data structure of the class within the method area, and provides the Java programmer with an interface to access the data structures within the method Area.

Life cycle of a class

The life cycle of a class consists of these parts, loading, connecting, initializing, using, and unloading, of which the first three are the loading processes of the class, such as;

    • load, Find and load the Class's binary data, and create an object of the Java.lang.Class class in the Java heap
    • connection, and the connection contains three pieces of Content: validate, prepare, initialize. 1) validation, file format, metadata, byte code, symbol reference verification, 2) prepare, allocate memory for static variables of class, initialize it to default value, 3) parse, convert symbol reference in class to direct reference
    • initialize, assigning the correct initial value to the static variables of the class
    • used, new Out-of-object programs are used
    • uninstall, Perform garbage collection

a few minor questions?
1. JVM Initialization steps? 2, class Initialization time? 3. in which cases will the Java Virtual machine end the life cycle?
Answer reference this article JVM series (i): Java class loading mechanism

Class Loader

    • Launch class Loader: Bootstrap ClassLoader, which is responsible for loading a class library that is stored in the Jdk\jre\lib (JDK represents the Jdk's installation directory, the same below), or in the path specified by The-xbootclasspath parameter, and can be recognized by the virtual machine
    • Extension class Loader: Extension ClassLoader, which is implemented by sun.misc.launcher$extclassloader, is responsible for loading the Dk\jre\lib\ext directory, or all class libraries in the path specified by the JAVA.EXT.DIRS system variable (such as classes beginning with javax.*), developers can use the Extension class loader directly.
    • Application Classloader: Application ClassLoader, which is implemented by sun.misc.launcher$appclassloader, is responsible for loading the class specified by the user class path (ClassPath). Developers can use the ClassLoader directly

Class loading mechanism

    • overall, when a classloader is responsible for loading a class, other classes that the class relies on and references will also be loaded by the classloader, unless the display uses a different classloader to load
    • The parent class delegate, which first lets the parent ClassLoader attempt to load the class, attempts to load the class from its own classpath only if the parent class loader cannot load the class
    • Caching mechanism, The caching mechanism will ensure that all the loaded class will be cached, when the program needs to use a class, the class loader first look for the class from the buffer, only the buffer does not exist, the system will read the corresponding binary data, and convert it into a class object, stored in the Buffer. This is why the JVM must be restarted after the class has been modified, and the Program's modifications will not take effect
JVM Memory Structure

Main points of Concern:

    • What are the JVM memory structures
    • Object Assignment Rules

JVM Memory Structure

The method area and the memory area that are shared by all threads, while the Java stack, the local method stack, and the Programmer's counter are areas of memory that are run as Thread-private.

    • The Java heap, which is the largest piece of memory managed by a Java virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is Started. The only purpose of this area of memory is to hold object instances where almost all of the object instances are allocated Memory.
    • Method area, The method area, like the Java heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual Machine.
    • The program counter (program Counter register), which is a smaller memory space, functions as a line number indicator of the byte code executed by the current Thread.
    • JVM Stack (JVM Stacks), like program counters, the Java Virtual machine stack (java Stacks) is also thread-private, with the same life cycle as the Thread. The virtual machine stack describes the memory model that is executed by the Java Method: each time a method is executed, a stack frame is created to store the local variable table, the operation stack, The dynamic link, the method exit, and so On. Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the STACK.
    • The local method stack (Native methods Stacks), The local method stack (Native methods Stacks) and the virtual machine stack play a very similar role, the difference is that the virtual machine stack for virtual machines to execute Java methods (that is, Bytecode) service, The local method stack is a service to the native method used by the virtual MACHINE.

Object Assignment Rules

    • The object is first allocated in the Eden area, and the virtual machine executes the minor GC once if there is not enough space in the Eden Area.
    • Large objects go directly into the old age (large objects are objects that require a large amount of contiguous memory space). This is done to avoid a large amount of memory copying between the Eden Zone and the two survivor zones (the Cenozoic uses a replication algorithm to collect memory).
    • Long-term survival of the object into the old Age. The virtual machine defines an age counter for each object, and if the object passes through 1 minor GC then the object will enter the survivor area, then each time after the minor GC so the Object's age plus 1, know to reach the threshold object into the old age Area.
    • Determines the age of the object dynamically. If the sum of all objects of the same age in the survivor area is greater than half the size of Survivor space, objects older than or equal to that age can enter the old age Directly.
    • Space allocation Guarantee. Each time the minor GC is performed, the JVM calculates the average size of the objects moved to the old age area of the survivor area, and if the value is greater than the remaining value of the old age area, a full GC is performed, and if it is less than the check handlepromotionfailure setting, If true, only the monitor GC is performed, and if False the full GC is Performed.

how to control each area of memory with parameters
refer to this article: JVM series (ii): JVM Memory Structure

GC Algorithm garbage Collection

Main points of Concern:

    • Object Survival judgment
    • GC algorithm
    • Garbage collector

Object Survival Judgment

There are two ways to determine if an object is Alive:

    • Reference Count: Each object has a reference count property, a new reference count plus 1, a reference release count minus 1, and a count of 0 can be Recycled. This method is simple and does not solve the problem of circular referencing between objects.
    • Accessibility Analysis: starting from the GC roots, the search is traversed by a path called a reachability chain. When an object is connected to a GC roots without any reference chain, it proves that the object is unavailable and Unreachable.

GC algorithm

Gc's most basic algorithm has three kinds: the tag-clear algorithm, the copy algorithm, the tag-compression algorithm, Our common garbage collector generally uses the generational collection Algorithm.

    • Mark-clear algorithm, the "mark-clear" (mark-sweep) algorithm, like its name, the algorithm is divided into "mark" and "clear" two stages: first mark out all the objects that need to be recycled, after the mark is complete, the Unified collection of all tagged Objects.
    • The copy algorithm, the "copy" (Copying) collection algorithm, divides the available memory by capacity into two blocks of equal size, using only one piece at a Time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once.
    • Tag-compression algorithm, The tagging process is still the same as the "mark-purge" algorithm, but the next step is not to clean up the recyclable objects directly, but to let all surviving objects move toward one end, and then directly clean out the memory outside the end boundary
    • The generational collection algorithm, the "generational collection" (generational Collection) algorithm, divides the Java heap into the new generation and the old age, so that the most appropriate collection algorithm can be used according to the characteristics of each age.

Garbage collector

    • The serial collector, the serial collector, is the oldest, most stable, and efficient collector, which may produce a longer pause, using only one thread to Recycle.
    • Parnew collector, The Parnew collector is actually a multithreaded version of the serial Collector.
    • Parallel collectors, Parallel Scavenge collectors resemble parnew collectors, and Parallel collectors pay more attention to the throughput of the System.
    • Parallel Old collector, Parallel OID is the older version of the Parallel scavenge collector, using multithreading and the "mark-and-organize" algorithm
    • The CMS collector, the CMS (Concurrent Mark Sweep) collector, is a collector that targets the shortest recovery pause time.
    • The G1 collector, G1 (garbage-first) is a server-oriented garbage collector for machines with multiple processors and large memory Capacity. High throughput performance characteristics while satisfying GC pause time requirements at a very high probability

GC algorithm and garbage collector algorithm plots and more detailed reference JVM series (iii): GC algorithm garbage collector

GC Analysis Command Tuning

Main points of Concern:

    • GC Log Analysis
    • Tuning commands
    • Tuning Tools

GC Log Analysis

Extract GC log part (green for young generation GC recycle; blue for full GC recycling):

Through the above log analysis, psyounggen, paroldgen, Pspermgen belong to the parallel collector. Which Psyounggen represents the memory change of the younger generation before and after GC recycling; Paroldgen represents the memory change of the old age before and after GC recycling; Pspermgen represents the memory change of the permanent area before and after GC Reclamation. Young GC is mainly for the younger generation of memory recovery is more frequent, time-consuming, full GC will be the entire heap of memory to return to the town, time consuming, so generally minimize the number of complete GC

Young GC Logs:

Full GC Log:

Tuning Commands

Sun JDK monitoring and troubleshooting commands have JPS Jstat jmap jhat jstack jinfo

    • The JPS,JVM process Status Tool displays all the hotspot virtual machine processes within the specified System.
    • The JSTAT,JVM statistics monitoring is a command used to monitor the state information of a virtual machine, and it can display runtime data such as Class loading, memory, garbage collection, JIT compilation, and so on in the process of virtual machines.
    • JMAP,JVM Memory Map command for generating heap dump files
    • The JHAT,JVM Heap Analysis tool command is used in conjunction with JMAP to analyze Jmap generated dump,jhat built-in a miniature http/html server that generates the results of the dump and can be viewed in a browser
    • jstack, which is used to generate a thread snapshot of the current moment of the Java virtual MACHINE.
    • JINFO,JVM Configuration Info This command is to view and adjust the virtual machine running parameters in real Time.

Detailed command use reference here JVM series (iv): JVM Tuning-command Post

Tuning Tools

The common tuning tools are divided into two categories, the JDK comes with monitoring tools: jconsole and jvisualvm, and third parties are: MAT (Memory Analyzer tool), gchisto.

    • Jconsole,java Monitoring and Management console is a Java monitoring and management console that comes with the JDK from java5, and is used to monitor memory, threads and classes in the JVM
    • Jvisualvm,jdk comes with an all-in-all tool to analyze memory snapshots, thread snapshots, monitor memory changes, GC changes, and More.
    • Mat,memory Analyzer tool, An eclipse-based memory analyzer, is a fast, feature-rich Java heap Analysis tool that can help us find memory leaks and reduce memory consumption
    • gchisto, a tool for professional analysis of GC logs

Tool use reference JVM series (vii): JVM Tuning-tools

a pure Smile
source: http://www.ityouknow.com/
copyright belongs to the author, please specify the source of the reprint

JVM Series (eight): JVM knowledge points overview-advanced Java Engineer Interview Prerequisites

Related Article

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.