Java virtual machine

summary

Java Official documents https://docs.oracle.com/en/java/index.html

JVM It's a norm , adopt Oracle Java Official documents found JVM Specification review of .Java Virtual machine can be regarded as a virtual computer , Body function bytecode instruction set ( assembly language ) And memory management ( Stack 、 Pile up 、 Method area ) etc.

common JVM Realization

  • Hotspot: The most widely used Java virtual machine .
  • Jrocket: Originally belongs to BEA company , Once known as the fastest in the world JVM, After being Oracle Company purchase , Merge in Hotspot
  • J9: IBM Have their own java Virtual machine implementation , It's called J9. Mainly for IBM product (IBM WebSphere and IBM Of AIX On the platform )
  • TaobaoVM: Only a certain volume 、 Only manufacturers of a certain scale will develop their own virtual machines , For example, Taobao has its own VM, It's actually Hotspot Customized version of , It's specially prepared for Taobao , Ali 、 God Cats use this virtual machine .
  • LiquidVM: It is a virtual machine for hardware , There is no operating system under it ( No Linux Neither windows), The following is the hardware , The operation efficiency is relatively high .
  • zing: It belongs to zual This company , Very good , It's a commercial product , Very expensive ! Its garbage collection speed is very fast (1 In milliseconds ), It is the industry benchmark . One of its garbage collection algorithms was later Hotspot Absorption makes the present ZGC.

Architecture

What we usually call JDK, In fact, it means Java Development kit , It contains Java Tool set used in development .

  • JDK Architecture

    • Java Running environment (JRE) And development tools ( compiler , The debugger ,javadoc etc. ).

      • JRE: from JVM,Java Runtime class library , Dynamic link library, etc ; It's for Java Provides the operating environment , One of the most important links is through JVM Interpret bytecode as executable machine code .
      • JDK The compiler Javac[.exe], Will Java Code compiled into bytecode (.class file ). The compiled bytecode has the same content on any platform , So we say Java Language is a cross platform language ;Writeonce, run anywhere.

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-2vmjOtk2-1644747481396)(http://www.itxiaoshen.com:3001/assets/1644552030402KQTxi6TP.png)]****

  • JVM Relationship with operating system

    • introduce Java After the language virtual machine ,Java The language does not need to be recompiled when it runs on different platforms .Java Language use Java Virtual machines block information about specific platforms , bring Java Language compilers only need to be generated in Java Target code running on virtual machine ( Bytecode ), It can run unmodified on multiple platforms .
    • In the runtime environment ,JVM Will Java Bytecode is interpreted as machine code . Machine code and platform related ( Different hardware environments 、 Different operating systems , The generated machine code is different ), therefore JVM There are different implementations on different platforms . at present JDK The default implementation is Hotspot VM.

  • JVM framework :Java Virtual machine is mainly divided into five modules : Class loader subsystem 、 Run time data area 、 Execution engine 、 Local method interface and garbage collection module .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Ut6GY2lN-1644747481400)(http://www.itxiaoshen.com:3001/assets/16445520369994chXCWt4.png)]

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-2TtOAZ9M-1644747481409)(http://www.itxiaoshen.com:3001/assets/1644641759500Hs1z7mMd.png)]

Escape analysis (Escape Analysis)

summary

  • Escape analysis is the dynamic scope of an object ,2 In this case

    • The way to escape : Object is passed to another method through parameters .
    • Thread escape feature : Object has another thread access .
  • The purpose of escape analysis is to confirm whether an object can only be accessed by the current thread .
  • Escape analysis can lead to a certain degree of performance optimization . But escape analysis itself also needs a series of complex analysis , This is also a relatively time-consuming process .
  • JIT(Just In Time Compiler) Just in time compilation technology , During instant compilation JVM We may do some optimization to our code, such as escape analysis .

Optimization strategy

There are three main optimization strategies for escape analysis : Objects may be allocated on the stack 、 Detach object or scalar substitution 、 Eliminate synchronization lock .

  • Objects may be allocated on the stack :JVM Through escape analysis , Analyze the scope of use of the new object , It is possible to allocate objects on the stack . Stack allocation can quickly create and destroy objects on stack frames , No longer allocate objects to heap space , Can effectively reduce JVM The pressure of recycling .
  • Detach object or scalar substitution : When JVM Through escape analysis , When you decide to assign an object to the stack , Just in time compilation can break up objects , Replace the object with a small local variable , We call this process of fragmentation scalar substitution . Replace the object with a local variable , It is very convenient to allocate on the stack . Scalars are variables that can no longer be divided , Such as Java Basic data type .
  • Synchronous lock elimination : If JVM Through escape analysis , It is found that an object can only be accessed from one thread , When accessing this object , Can not add synchronization lock . If used in the program synchronized lock , be JVM Will synchronized Lock elimination .

Escape analysis switch

Escape analysis is not a new concept , As early as 1999 In, a paper proposed this technology . But in Java It is a novel and foreword Optimization Technology , from JDK1.6 Just started introducing this technology ,JDK1.7 Start with escape analysis enabled by default , It can also be controlled by a switch

  • -XX:+DoEscapeAnalysis Open escape analysis
  • -XX:-DoEscapeAnalysis Close escape analysis
  • -XX:+EliminateAllocations Turn on scalar substitution
  • -XX:-EliminateAllocations Close scalar substitution
  • -XX:+EliminateLocks Open lock to eliminate
  • -XX:-EliminateLocks Close the lock to eliminate
  • Turn on scalar substitution or lock elimination The escape analysis switch must be turned on

Common interview questions

  • Will all objects and arrays allocate space in heap memory ? not always , As long as you master the principle of escape analysis , Then you know clearly Java Objects in are not necessarily allocated on the heap , because JVM Through escape analysis , Be able to analyze the scope of use of a new object , And use this to determine whether to allocate this object to the heap .

  • Is the code lock with lock bound to take effect ? not always

JVM Operation mode

  • Explain the pattern (Interpreted Mode): Just use the interpreter (-Xint mandatory JVM Use the interpretive pattern ), Execute a row JVM Bytecode compiles a line of machine code
  • Compile mode (Compiled Mode): Just use the compiler (-Xcomp JVM Use compile mode ), First of all JVM Bytecode is compiled into machine code at a time , Then execute all machine codes at once
  • Mixed mode (Mixed Mode):(-Xmixed Set up JVM Use mixed mode ) Still use interpretation mode to execute code , But for some “ hotspot ” The code is executed in compiler mode , The machine code corresponding to these hotspot codes will be cached , There is no need to compile next time .JVM In general, mixed mode is used to execute code
JVM Operation mode advantage Applicable scenario
Explain the pattern Fast start Just execute part of the code , And most of the code will be executed only once
Compile mode Slow start , But the execution speed is fast in the later stage , Compare memory usage 1 Suitable for scenarios where code may be executed repeatedly
Mixed mode commonly JVM The default mode

Java Start parameter classification

  • Standard parameters (-), be-all JVM The implementation must realize the function of these parameters , And backward compatibility ;
  • Nonstandard parameters (-X), Default jvm Realize the function of these parameters , But there is no guarantee that all jvm The realization is satisfied , And there is no guarantee of backward compatibility ;jvm Key points of parameter tuning
  • Not Stable Parameters (-XX), Each of these parameters jvm The implementation will be different , It may be cancelled at any time in the future , It needs to be used carefully ;jvm Key points of parameter tuning . Can pass java -XX:+PrintFlagsFinal -version Get support parameter options .

Class loading

Class loading process

  • Class loading : Class loader will class The file is loaded into the memory of the virtual machine

  • Class 7 Life cycles : load -> verification -> Get ready -> analysis -> initialization -> Use -> uninstall

    • load : Find it on the hard disk and go through IO Read in bytecode file

    • Connect : Execution verification 、 Get ready 、 Parsing steps

      • verification : Check the correctness of bytecode file , It's not necessary , You can turn off verification to improve the speed of virtual machine loading classes
      • Get ready : Allocating memory to static variables of a class , And give default values
      • analysis : Replace symbol references with direct references ; adopt javap -v Disassemble the bytecode file into a more readable file , Image class , Methods and other literal quantities are converted into symbolic references #1...N. Including class parsing , Field analytical , Method resolution , Interface analysis . That is, the static literal Association of bytecode ( character string , static state ) Convert to JVM Dynamic pointer Association in memory ( The pointer , dynamic ).

    • initialization : Class static variables are initialized to the specified values , Execute static code block

    • Use :new Used in the object program
    • uninstall : Carry out garbage collection

Class loader

summary

  • Start class loader :C Language development , load Java The core library . Based on sandbox mechanism , Load only java、javax、sun Class at the beginning of package .
  • Extend the classloader :Java Language writing , from sun.misc.Launcher$ExtClassLoader Realization , The upper loader is the boot class loader , Responsible for loading jre/lib/ext Expand... In the directory jar Class package .
  • Application class loader :Java Language writing , from sun.misc.Launcher$AppClassLoader Realization , The upper loader is the extension class loader , Is the default class loader . Responsible for loading ClassPath Class package under path , The main thing is to load the classes you write .
  • Custom class loaders : Responsible for loading the class package under the user-defined path
    • Use scenarios : Bytecode binary streams come from the network , Bytecode file is not in the specified lib、ext、classpath Under the path , You need to process the binary stream to get the bytecode .
    • Different class loaders load the same Class After the bytecode file , stay JVM The class objects generated in are different . The same classloader ,Class Examples are in JVM China is the only one .
    • stay Java in , A class uses its Fully qualified class name ( Include package name and class name ) As identification ; But in JVM in , A class is uniquely identified by its fully qualified class name and its class loader .

Class loader get example

JVM The predefined class loader is the boot class loader 、 Extend the classloader 、 Application class loader .

package cn.itxs.classloader;

import cn.itxs.entity.User;

public class ClassLoaderMain {
public static void main(String[] args) {
System.out.println(String.class.getClassLoader());
System.out.println(com.sun.crypto.provider.DESedeKeyFactory.class.getClassLoader().getClass().getName());
System.out.println(User.class.getClassLoader().getClass().getName());
System.out.println(ClassLoader.getSystemClassLoader().getClass().getName());
}
}

Custom class loader example

In addition to starting the classloader , All classloaders are ClassLoader Subclasses of , So we can inherit ClassLoader To implement your own classloader .

package cn.itxs.entity;

public class Goods {
public void sayHello(){
System.out.println("hello goods!");
}
}

Generate bytecode file Goods.class Post shear ( It's not a copy , Delete the bytecode file under the classpath ) To D:\temp\cn\itxs\entity Under the table of contents

package cn.itxs.classloader;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel; public class ItxsClassLoader extends ClassLoader{
private String classPath; public ItxsClassLoader(String classPath) {
this.classPath = classPath;
} private byte[] loadBytes(String name) throws IOException {
name = name.replaceAll("\\.","/");
FileInputStream fis = new FileInputStream(classPath+"/"+name+".class");
FileChannel fc = fis.getChannel();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
WritableByteChannel wbc = Channels.newChannel(baos);
ByteBuffer by = ByteBuffer.allocate(1024);
while (true){
int i = fc.read(by);
if (i == 0 || i == -1){
break;
}
by.flip();
wbc.write(by);
by.clear();
}
fis.close();
return baos.toByteArray();
} @Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
byte[] bytes = loadBytes(name);
return defineClass(name,bytes,0, bytes.length);
}catch (Exception e) {
e.printStackTrace();
throw new ClassNotFoundException();
}
} public static void main(String[] args) throws Exception {
ItxsClassLoader itxsClassLoader = new ItxsClassLoader("D:/temp");
Class clazz = itxsClassLoader.loadClass("cn.itxs.entity.Goods");
Object obj = clazz.newInstance();
Method method = clazz.getDeclaredMethod("sayHello", null);
method.invoke(obj,null);
System.out.println(clazz.getClassLoader().getClass().getName());
}
}

Parent delegate mechanism

  • Parent delegate mechanism : When a class loader receives a class load request , It doesn't load the specified class directly , Instead, delegate the request to your parent loader to load it . Only when the parent loader cannot load this class , The current loader will be responsible for loading the class .
  • When loading classes , Although user-defined classes will not be used by Bootstrap ClassLoader or Extension ClassLoader load ( Determined by the loading range of the class loader ), But the code implementation will still be delegated until Bootstrap ClassLoader, The upper layer cannot load , Then whether the lower layer can load , If you can't load , It will trigger findClass, Throw out ClassNotFoundException.
  • The hierarchical relationship between class loaders does not exist in the form of inheritance , But in combination .

  • The parental delegation mechanism is in ClassLoader Inside loadClass Method , If you want to implement the class loader yourself , Inherit ClassLoader After rewriting findClass Method , Load the corresponding class . The implementation process of the source code is as follows :

    • First, judge whether the class has been loaded .
    • The class is not loaded , If the parent class is not empty , Give it to the parent class to load .
    • If the parent class is empty , hand Bootstrap Classloader load .
    • If the class still cannot be loaded into , The trigger findClass, Throw out ClassNotFoundException.
    protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
} if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name); // this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
  • The role of parental delegation mechanism

    • It can avoid the repeated loading of classes , When the parent loader has loaded a class , The child loader will not reload the class .
    • Safety guaranteed . because Bootstrap ClassLoader When loading , Only... Will be loaded JAVA_HOME Medium jar The class in the bag , Such as java.lang.Integer, This class will not be replaced at will , This can effectively prevent the core Java API Be tampered with .
  • Break parental delegation

    • The parental delegation mechanism is not inevitable . such as Tomcat web There are many applications deployed in the container , The dependent versions of these applications on third-party class libraries are different , And usually the path of these third-party class libraries is the same , If you use the default parental delegation class loading mechanism , You can't load more than one of the same classes . therefore Tomcat Undermining the principle of parental appointment , Provide a mechanism for isolation , For each web The container provides a separate WebAppClassLoader loader .
    • Tomcat Class loading mechanism of : To achieve isolation , Priority load Web Apply your own defined classes , Do not follow the appointment of parents , Each application has its own class loader ——WebAppClassLoader Responsible for loading the class file , If you can't load it, give it to CommonClassLoader load .

** My blog website **IT Little god www.itxiaoshen.com

JVM Performance tuning and practical basic theory - More related articles on

  1. JVM Performance tuning in practice : Use Alibaba open source tools TProfiler Accurately locate performance code in massive business code

    This article is about <JVM Performance tuning in practice : A system performance bottleneck search process > The follow-up to , This article describes how to use JDK Self provided tools for JVM Tuning will TPS from 2.5 Upgrade to 20 ( Promoted 7 times ) ...

  2. JVM Performance tuning in practice : A system performance bottleneck search process

    Anyone who has played performance optimization knows , The key to performance optimization is not how to optimize , But how to find the performance bottleneck of the current system . Performance optimization is divided into several levels , For example, system level . Algorithm level . Code level …JVM Performance optimization is considered to be the underlying optimization , High threshold , ...

  3. JVM Performance tuning practices ——JVM piece

    Preface When it comes to actual performance problems , In addition to focusing on system performance metrics . Also combine the application's system log . Stack information .GClog.threaddump And other data for problem analysis and positioning . For performance index analysis, please refer to the previous article JVM Performance tuning practices -- ...

  4. Use Alibaba open source tools TProfiler Accurately locate performance code in massive business code (jvm performance tuning )

    Technology exchange group :233513714 This article is about <JVM Performance tuning in practice : A system performance bottleneck search process > The follow-up to , This article describes how to use JDK Self provided tools for JVM Tuning will TPS from 2.5 ...

  5. JVM performance tuning (3) —— Memory allocation and garbage collection tuning

    Preface article : JVM performance tuning (1) -- JVM Memory model and class loading mechanism JVM performance tuning (2) -- Garbage collectors and recycling strategies One . The goal of memory tuning The new generation of garbage collection is relatively simple ,Eden The zone is full and cannot assign new objects ...

  6. JVM performance tuning (4) —— Performance Tuning Tools

    Preface article : JVM performance tuning (1) -- JVM Memory model and class loading mechanism JVM performance tuning (2) -- Garbage collectors and recycling strategies JVM performance tuning (3) -- Memory allocation and garbage collection tuning One .JDK Tools Let's see if there are ...

  7. How to plan reasonably once jvm performance tuning

    https://blog.csdn.net/miracle_8/article/details/78347172 Abstract : JVM Performance tuning involves all kinds of trade-offs , It's often a hair that moves the whole body , We need to consider all aspects of ...

  8. How to plan reasonably jvm performance tuning

    JVM Performance tuning involves all kinds of trade-offs , It's often a hair that moves the whole body , We need to consider all aspects of the impact . But there are also some basic theories and principles , Understanding these theories and following these principles will make your performance tuning task easier . In order to better understand the inner ...

  9. JVM Performance tuning details

    We learned the whole before JVM series , The ultimate goal is not just to understand JVM Basic knowledge of , It's also for the sake of JVM Prepare for performance tuning . This article leads you to learn JVM Knowledge of performance tuning . performance tuning Performance tuning consists of several layers , such as : Architecture tuning . Code ...

  10. [ Re posting ]JVM Performance tuning details

    JVM Performance tuning details https://www.cnblogs.com/secbro/p/11833651.html Should be jdk8 The old way Looks like permsize I've given up this piece . We learned from ...

Random recommendation

  1. desire VS resist

    There are always many things brewing in my heart for a long time , But I really want to do it, but I'm very resistant to it . I'm afraid , Still running away ? From the heart , happy , It's all so hard .

  2. iOS Error report in development compilation 、 common problem ( Real time updates )

    One . Errors and alarms 1. Error code :No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCH ...

  3. EntityFramework The creation of database and basic operation ( One )

    I learned at that time EF It wasn't before Code First, Only DB First, What is generated is a EDMX file ,Code First There is no such document , Let's go straight to the code Database creation and table creation 1. First, let's create a new project , Use Nu ...

  4. SQL Study and work daily : Sentence accumulation

    1. Cross server connection data table -- Open the server configuration 'Ad Hoc Distributed Queries' --exec sp_configure 'show advanced options',1 --re ...

  5. HDOJ/HDU 2549 It's hard to pay for ambition ( Take a few decimal places ~)

    Problem Description Words MCA All the heroes on the mountain went out to fight against the enemy , He was famous in the river last year , Jianghu people < Take the first rank of the general from ten thousand troops > I'm not willing to be outdone ," It will be a great task for us , Must first ...

  6. Wireshark Grab the bag 、 filter

    Refer to http://blog.sina.com.cn/s/blog_5d527ff00100dwph.html 1. Capture filters The steps to set the snap filter are :- choice capture -> optio ...

  7. FPGA Of LE The relationship between number and gate number ( turn )

    generally speaking FPGA There are two ways to calculate the number of equivalent gates One is to put the FPGA Basic unit ( Such as LUT+FF,ESB/BRAM) Compared with the standard gate array that realizes the same function , The number of gates contained in the gate array is this FPGA Equivalent gate number of basic unit , Then multiply by the basic unit ...

  8. js Generate captcha and judge

       <style type="text/css">        .code        {            font-family: Arial;      ...

  9. IT Why is there no progress in the industry

    I've been working for many years , Back and forth N Multiple projects , Most of them are government bidding projects . Each project is accepted smoothly , Most of these accepted projects are more or less delayed , Each project can correspond to the contract or requirements specification , But really ...

  10. CSS3 Summarize learning ( One ):CSS3 The user interface

    stay CSS3 in , There are many new user interface properties , This article focuses on resize,box-sizing,offset. Browser support , Here's the picture , Pictures originate from W3school 1.CSS Resizing stay css3,resiz ...