Article about the important components of a Java virtual machine

Source: Internet
Author: User
Tags stack trace

JVM is one of the important components of the Java platform, because it involves too many points of knowledge, so from the following aspects of the JVM for a shallow introduction, if you need to understand deeply, it is recommended to learn the mechanical industry press "in-depth understanding of Java Virtual Machine."

First, Java memory structure

The JVM runtime data area as specified in the Java Virtual Machine specification is as follows:

Overall, there is a thread-sharing section (method area, heap) and thread isolation (virtual machine stack, local method stack, and program counter).

1. Method area

Used to store data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine. Where constants are stored in the run-time-constant area, which is part of the zone that stores the literal and symbolic references generated by the compilation period. However, the contents of the run-time zone are not only generated during compilation, but can also be added to the constant area at runtime by String.intern ().

2. Heap

is the largest chunk of memory in the JVM, and the purpose of this area is to store object instances and arrays. This area is also the most important area of GC.

3. Virtual Machine Stack

Each threading method creates a stack frame at execution time, containing information such as the local variable table, the return address, the operand stack, and so on. The execution and completion of each method corresponds to the stack frame in the stack and out of the stack process. The size of the local variable table footprint is determined at compile time.

4. Local Method Stack

Similar to the virtual machine stack, where execution is a local method. For a hotspot virtual machine, the local method stack and the virtual machine stack are unified.

5. Program counter

is a small memory space, if the thread is executing a Java method, this memory region records the virtual machine bytecode instruction being executed, or if the thread is executing the native method, the value in the calculator is empty.

Second, Java garbage collection mechanism

Java garbage collection mainly involves determining whether the object is alive, garbage collection and other algorithms, in which the object recovery algorithm is based on the accessibility analysis algorithm, garbage collected at present, the JVM manufacturers are widely used in the generation of collection algorithm. This is the main description of the next generation of collection algorithm process.

The core idea of the generational collection algorithm is to divide the memory area into the life cycle phase of the object, in which the heap is divided into the Cenozoic (young generation) and the old generation (older generation). The non-heap area (generally referred to as the method area) is divided into a durable generation (permanent generation).

1. Cenozoic

The new generation can be subdivided into Eden and two survivor (two survivor districts are the same size, easy to exchange). The newly generated objects will be saved in the new generation of Eden area first. The new generation is characterized by a large amount of memory being recycled per garbage collection and frequently collected, so the new generation is suitable for the following collection algorithms:

First, the newly generated object is assigned to the Eden area, and if the Eden area is full, the accessibility object is copied to the Survivor1 area and the Eden area is emptied.

Then, if the Survivor1 area is full, copy the Eden and Survivor1 area's accessibility objects to the Survivor2 area, then empty the Eden and Survivor1 areas, and then swap the Survivor2 zone with the Survivor1 area after emptying. That is, keep the survivor2 empty.

Once again, if the Survivor2 area is full, the accessibility objects of the Eden, Survivor1, and Survivor2 areas are copied into the old age and emptied in the Cenozoic.

Finally, the full GC is triggered if the old age is filled.

2. The old age

The memory of the old age is much larger than that of the Cenozoic, and the area of garbage collection is not high. The full GC is triggered when the old age is filled.

3. Persistent generation

A durable generation generally refers to a method area, which needs to be recycled with discarded constants and classes. For a constant to be recoverable by the method of accessibility analysis, the following conditions are required for the class to be recycled:

First, all instance objects of the class have been recycled;

Second, the class loader for this class has also been recycled;

Again, the class method is not referenced anywhere, that is, it is not possible to access the method of the class by reflection at any place.

4. When will the garbage collection be resolved?

In summary, when Eden is full, the scavenge GC is triggered, and when the following conditions are present, the fully GC is triggered:

The old age is full;

The permanent generation is full;

Call the System.GC () method;

Third, Java class loading process

The JVM class loading process is loaded, validated, prepared, parsed, and initialized in five parts.

1. Loading

During the loading process, the following things need to be done:

1) Obtain the binary byte stream of the class by the fully qualified name of the class;

2) Convert the binary byte stream of the class into the runtime data structure of the method area;

3) Generate a Java.lang.Class object representing this class as the access entry for various data of this class of method area.

2. Verification

Validation, parsing, and initialization are also known as the connection phase, where validation is primarily to ensure that the binary byte stream conforms to the JVM's specifications without compromising the security of the computer. The specific verification phase needs to be done as follows:

1) file format verification, verify that the byte stream conforms to the class file format specification;

2) meta-data validation, semantic verification of bytecode to ensure that its descriptive information conforms to the Java language specification;

3) Byte code verification, through data flow and control flow analysis, to determine the program semantics is legal, logical;

4) symbol reference validation, matching validation of information referenced by various symbols in a constant pool.

3. Preparation

The process of preparing is actually the process of allocating memory. There are two confusing concepts at this stage: one is the class variable (static variable) that allocates memory at this stage, does not contain an instance variable, the memory allocation of an instance variable is allocated in the heap with the object when the object is instantiated, and the second is that the value stored in the memory of the stage is only 0 of the data type. The value needs to be assigned during the initialization phase. There is also a special case where a static constant (final decoration) assigns a value to a real value during the preparation phase.

4. Parsing

The parsing phase is the process of converting a symbolic reference in a constant pool into a direct reference, including parsing of classes and interfaces, parsing of fields, parsing of methods, interface methods and parsing.

5. Initialization

The initialization phase is actually the stage of executing the class constructor (Clinit). The following points need to be explained for Clinit ():

1) The program in Clinit () is generated by automatically collecting static variables and static blocks in the class, and the order of execution is consistent with the order in the code. A static statement block can only access the static variable declared before it, and the static variable declared after it can only be assigned a value and cannot be accessed.

2) before executing the Clinit () method, the JVM automatically calls the parent class's Clinit () method;

3) Virtual opportunity to ensure that a class of clinit () in a multithreaded environment, automatic locking, synchronization.

Iv. class loader for the JVM

Class loading of the JVM is implemented through the ClassLoader, and the commonly used ClassLoader includes the following three types:

1. Start the class loader (bootstrap ClassLoader): Load class under {Jdk_home}/lib

2. Extension class loader (extension ClassLoader): Load class under {Jdk_home}/lib/ext

3. Application class loader (application ClassLoader): Load Classpath specified class

You can refer to the following parental delegation model for different classloader and for collaboration between them.

The working process of the parent delegation model is that if a class loader receives a load request for a class, it first delegates the request to its own parent class, which is the case for each level of the ClassLoader, because all the load requests are eventually sent to the Bootstarp loader, only if the parent loader does not complete its own load request , the child loader will attempt to load itself.

The parental delegation model allows Java classes to load hierarchically, without causing confusion.

V. Related tools for the JVM

There are many powerful monitoring tools in the JDK that can be run directly from the command line. This is useful for monitoring in a production environment. For example, the following monitoring and troubleshooting tools are included in the sun JDK.

JPS:JVM Process Status tool that displays all the hotspot virtual machine processes within the specified system
JSTAT:JVM Statistics Monitoring tool for collecting operational data for all aspects of the hotspot virtual machine
Jinfo:configuration info for Java, displaying virtual machine configuration information
Jmap:memory map for Java, generating a memory dump snapshot of a virtual machine (heapdump file)
JHAT:JVM Heap Dump Browser, used to parse the Heapmap file, establishes a http/html server that allows the user to view the results of the analysis on the browser
Jstack:stack Trace for Java, displaying a thread snapshot of a virtual machine

With this in front of you, you'll have the opportunity to introduce the JVM's support for concurrency in the future.

Article about the important components of a Java virtual machine

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.