? Study Notes on JVM Working Principles

Source: Internet
Author: User

 

I,JVMLifecycle

1. The JVM instance corresponds to a Java program that runs independently. It is a process-level program.

A) Start. When a Java program is started, a JVM instance is generated. Any class with the public static void main (string [] ARGs) function can be used as the starting point for running the JVM instance.

B) run. Main () is the starting point of the program's initial thread, and any other threads are started by this thread. There are two types of threads in the JVM: the daemon thread and the non-daemon thread. The main () is a non-daemon thread, which is usually used by the JVM itself, the Java program can also indicate that the thread created by itself is a daemon thread.

C) extinction. JVM exits only when all non-daemon threads in the program are terminated. If the security manager permits, the program can also exit using the runtime class or system. Exit ().

2. The JVM execution engine instance corresponds to the thread of the user running program. It is a line level.

 

II,JVMArchitecture

 

 

1. classloader (used to load. class files)

2. execution engine (execute bytecode or execute local methods)

3. runtime data zone (method zone, heap, java stack, PC register, local method stack)

 

III,JVMClass Loader

JVMSteps for the entire class loading process:

1.Load

The loading process is responsible for finding the binary bytecode and loading it to JVM. The JVM loads the class through the class name and the class package name through classloader. Similarly, the preceding three elements are used to identify a loaded class: class name +

Package name + classloader instance id.

2.Link

The link process verifies the binary bytecode format, initializes static variables in the loading class, and interfaces and classes called in the parsing class.

After the verification is completed, the static variables in the JVM initialization class are assigned the default value.

Finally, all the attributes and methods in the class are verified to ensure that the attributes and methods to be called exist and have the necessary permissions (such as public and private domain permissions ), it will cause errors such as nosuchmethoderror and nosuchfielderror.

3.Initialization

The initialization process is the initialization of static initialization code, constructor code, and static attributes in the execution class. In four cases, the initialization process is triggered and executed:

New;

Reflection calls methods in the class;

The subclass calls initialization;

Specifies the initialization class during JVM startup.

 

JVMClass loading sequence:

 

JVMThe following two types of loaders are available:Start the class loader and user-defined class loader.

Starting the class loader is part of the JVM implementation;

The user-defined classloader is part of the Java program and must be a subclass of the classloader class.

JVMLoading sequence:

When JVM is started, bootstrap loads classes to user-defined;

When the application performs classloader, user-defined searches for and loads classes in the bootstrap direction;

1.Bootstrap classloader

This is the root classloader of JVM. It is implemented in C ++. This classloader is initialized during JVM startup, and JRE/lib/RT in $ java_home is completed by classloader. jar (Sun JDK implementation) is used to load all class files. This jar contains all interfaces and implementations defined in Java specifications.

2.Extension classloader

JVM uses this classloader to load some jar packages of the extended function.

3.System classloader

JVM uses this classloader to load the jar package and directory in the classpath specified in the startup parameter. In Sun JDK, the class corresponding to classloader is named appclassloader.

4.User-Defined classloader

User-definedclassloader is a self-implemented classloader inherited by Java developers from the classloader abstract class. User-Defined classloader can be used to load jar and directories in non-classpath.

 

ClassloaderSeveral key methods of abstract classes:

(1) loadclass

This method is used to load the class with the specified name. The Implementation Method of classloader is to find the class from the loaded class. If not, continue searching from the parent classloader. If it is still not found, find from system classloader and call the findclass method. To change the class loading sequence, override this method.

(2) findloadedclass

This method searches for loaded classes from the cache of the current classloader Instance Object and calls the native method.

(3) findclass

This method directly throws classnotfoundexception, so you need to overwrite loadclass or this method to load the corresponding class in a custom way.

(4) findsystemclass

This method is used to search for classes from the system classloader. If the class is not found, it continues searching from the bootstrap classloader. If the class is still found, null is returned.

(5) defineclass

This method converts binary bytecode to class objects.

(6) resolveclass

This method completes the link of the class object. If the link is already linked, the system returns the result directly.

 

IV,JVMExecution engine

JVM during method executionFour commands are provided for execution:

(1) invokestatic: Call the static method of the class

(2) invokevirtual: Call the method of the object instance

(3) invokeinterface: defines attributes as interfaces for calling.

(4) invokespecial: When JVM initializes an object (Java constructor method: <init>) and calls a private method in an object instance.

 

The main execution technologies include:

Explanation, real-time compilation, adaptive optimization, and Chip-level direct execution

(1) The interpretation belongs to the first generation JVM,

(2) instant JIT compilation is the second generation JVM,

(3) Adaptive Optimization (Currently Sun's hotspotjvm adopts this technology) is based on the first-generation JVM and the second-generation JVM.

JVM experience, using a combination of the two

Start to explain and execute all the code and monitor the code execution. Then, start a background thread for frequently called methods and compile it into local code, and optimization. If the method is no longer frequently used, the compiled code is canceled and its interpretation is still executed.

 

V,JVMRuntime data Zone

 

First: PCRegister

The PC register is used to store the JVM commands that each thread will execute next. If this method is native, no information is stored in the PC register.

Part 2: JVMStack

The JVM stack is private to the thread. a jvm stack is created for each thread at the same time, variables stored in the JVM Stack are local basic types in the current thread (eight basic types defined in Java: Boolean, Char, byte, short, Int, long, float, double) partial returned results and stack frame. Non-basic types of objects only store one address pointing to the stack on the JVM stack.

Part 3: heap)

It is the region where JVM is used to store object instances and array values. It can be considered that the memory of all objects created through new in Java is allocated here, and the memory of objects in heap needs to be recycled by GC.

 

(1) The heap is shared by all threads in the JVM. Therefore, the object memory allocation on the heap needs to be locked. This also causes the overhead of the new object to be relatively large.

(2) In order to improve the efficiency of Object Memory Allocation, Sun hotspot JVM allocates an independent space for the created thread tlab (Thread Local allocation buffer ), the size is calculated by the JVM Based on the running situation. No lock is required when objects are allocated on the tlab. Therefore, the JVM tries its best to allocate the memory to the objects in the thread on the tlab, in this case, the performance of allocating Object Memory in JVM is basically the same as that of C, but if the object is too large, the heap space allocation is still used directly.

(3) tlab only applies to the new generation of Eden space. Therefore, when writing Java programs, it is generally more efficient to allocate multiple small objects than large objects.

Area 4: Method Area)

(1) In Sun JDK, this region corresponds to permanetgeneration, also known as permanent generation.

(2) The method area stores the information of the loaded class (name, modifier, etc) static variables in the class, constants defined in the class as final type, field information in the class, and method information in the class.

When getname, isinterface, and other methods in the object are used to obtain information, the data comes from the method region, and the method region is also shared globally. Under certain conditions, the data is also GC, when the memory used in the method area exceeds the maximum size, an outofmemory error message is thrown.

Fifth: runtime constant pool)

It stores fixed constant information, method and field reference information in the class, and its space is allocated from the method area.

Block 6: native method stacks)

JVM uses the local method stack to support native method execution. This region is used to store the status of each native method call.

 

VI,JVMGarbage Collection

GCBasic Principles:It recycles objects that are no longer used in memory. GC is called a collector. GC consumes some resources and time, after analyzing the lifecycle features of an object, Java collects the object based on the new generation and old generation methods to minimize the pause caused by GC on the application.

(1) collection of new generation objects is called minor GC;

(2) collection of old generation objects is called full GC;

(3) The GC that actively calls system. GC () is executed is full GC.

Different object reference types, GCDifferent methods will be used for collection, JVMThere are four types of object references:

(1) strong reference: by default, all objects use strong references (the instances of this object are not referenced by other objects and will be recycled only when GC is used)

(2) soft reference: soft reference is an application provided by Java that is suitable for cache scenarios (GC is performed only when the memory is insufficient)

(3) Weak reference: It will be recycled by GC.

(4) Virtual Reference: Because the Virtual Reference is only used to know whether the object is GC

? Study Notes on JVM Working Principles)

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.