当前位置:网站首页>JVM the truth you need to know

JVM the truth you need to know

2022-07-07 05:42:00 Jonny Jiang-zh

jvm The truth you need to know

class Approximate file loading process

 Insert picture description here

common jvm

  • hotspot
  • jrocket——IBM
  • Microsoft vm
  • taobao vm
  • azul zing

class file format

compile :

javac xxx.java

see :

  1. javap -v xxx.class
  2. idea plug-in unit jclasslib
 File identifier | The child version number | Major version number | Number of constant pool elements 
cafe babe 0000 0037 0010 0a00 0300 0d07
000e 0700 0f01 0006 3c69 6e69 743e 0100
0328 2956 0100 0443 6f64 6501 000f 4c69
6e65 4e75 6d62 6572 5461 626c 6501 0012
4c6f 6361 6c56 6172 6961 626c 6554 6162
6c65 0100 0474 6869 7301 000b 4c6a 766d
2f44 656d 6f31 3b01 000a 536f 7572 6365
4669 6c65 0100 0a44 656d 6f31 2e6a 6176
610c 0004 0005 0100 096a 766d 2f44 656d
6f31 0100 106a 6176 612f 6c61 6e67 2f4f
626a 6563 7400 2100 0200 0300 0000 0000
0100 0100 0400 0500 0100 0600 0000 2f00
0100 0100 0000 052a b700 01b1 0000 0002
0007 0000 0006 0001 0000 0009 0008 0000
000c 0001 0000 0005 0009 000a 0000 0001
000b 0000 0002 000c 

jdk+jre+jvm The relationship between

 Insert picture description here

Class loading process ( Class file +class object )

 Insert picture description here

  1. loading: Load the bytecode file into memory
  2. linking: Be responsible for the operation before initializing the class
    1. verification: Verify whether the document is formal class file ( Verify bytecode file )
    2. prepare: Assign default values to class attributes
    3. resolution: Convert symbolic references to Memory reference
  3. initialization: Assign the initial value to the attribute in the class

Class loader

classification

 Insert picture description here

Execute the process ( Parent delegate mechanism )

 Insert picture description here

Loading paths of different class loaders

  1. By looking at “Launcher” The source code of class , The range of different loaders is as follows :
    • boostrap => sun.boot.class.path
    • extension => java.class.path
    • application => java.ext.dirs
  2. Run the response code to see
public class Demo1 {
    
    public static void main(String[] args) {
    
        System.out.println(System.getProperty("sun.boot.class.path").replaceAll(";",System.lineSeparator()));
        System.out.println();
        System.out.println(System.getProperty("java.class.path").replaceAll(";",System.lineSeparator()));
        System.out.println();
        System.out.println(System.getProperty("java.ext.dirs").replaceAll(";",System.lineSeparator()));
    }
}
D:\Java\jdk1.8.0_291\jre\lib\resources.jar
D:\Java\jdk1.8.0_291\jre\lib\rt.jar
D:\Java\jdk1.8.0_291\jre\lib\sunrsasign.jar
D:\Java\jdk1.8.0_291\jre\lib\jsse.jar
D:\Java\jdk1.8.0_291\jre\lib\jce.jar
D:\Java\jdk1.8.0_291\jre\lib\charsets.jar
D:\Java\jdk1.8.0_291\jre\lib\jfr.jar
D:\Java\jdk1.8.0_291\jre\classes

D:\java\jdk1.8.0_291\jre\lib\charsets.jar
D:\java\jdk1.8.0_291\jre\lib\deploy.jar
D:\java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar
D:\java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar
D:\java\jdk1.8.0_291\jre\lib\ext\dnsns.jar
D:\java\jdk1.8.0_291\jre\lib\ext\jaccess.jar
D:\java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar
D:\java\jdk1.8.0_291\jre\lib\ext\localedata.jar
D:\java\jdk1.8.0_291\jre\lib\ext\nashorn.jar
D:\java\jdk1.8.0_291\jre\lib\ext\sunec.jar
D:\java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar
D:\java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar
D:\java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar
D:\java\jdk1.8.0_291\jre\lib\ext\zipfs.jar
D:\java\jdk1.8.0_291\jre\lib\javaws.jar
D:\java\jdk1.8.0_291\jre\lib\jce.jar
D:\java\jdk1.8.0_291\jre\lib\jfr.jar
D:\java\jdk1.8.0_291\jre\lib\jfxswt.jar
D:\java\jdk1.8.0_291\jre\lib\jsse.jar
D:\java\jdk1.8.0_291\jre\lib\management-agent.jar
D:\java\jdk1.8.0_291\jre\lib\plugin.jar
D:\java\jdk1.8.0_291\jre\lib\resources.jar
D:\java\jdk1.8.0_291\jre\lib\rt.jar
D:\codes\java\review\out\production\review
D:\IntelliJIDEA2021.1.1\lib\idea_rt.jar

D:\Java\jdk1.8.0_291\jre\lib\ext
C:\WINDOWS\Sun\Java\lib\ext

Process finished with exit code 0

Custom class loaders

Inherit classLoader Re actualize findClass, The byte stream read uses defineClass

jvm By default, the compilation interpretation is used to execute at the same time

  • -Xmix: Specify to execute in a mixed way
  • -Xint: Specify to execute in an interpreted way
  • -Xcomp: Specifies to execute

jvm Specify four cases of loading classes

  1. new getstatic putstatic invokestatic Instructions , visit final Variable
  2. java.lang.reflect Make a reflection call to the class
  3. When initializing a subclass , The parent class first initializes
  4. Dynamic language support java.lang.invoke.MethodHandle The result of analysis is REF_getstatic REF_putstatic REF_invokestatic Method handle of

Four ways to create objects

  1. new
  2. Reflection
  3. clone
  4. serialize

Parent delegate mechanism

break ( be based on jvm In the load .class A copy of the file will be saved in memory .class file + class object )

  1. how : rewrite classloader Of loadclass Method
  2. Why? : Due to the existence of parental delegation mechanism, when loading classes with the same path, we always get the class loaded in memory during the loading process , Can't get the latest in time
  3. when :
    1. jdk1.2 You have to
    2. ThreadContextClassloader
    3. tomat Of classloader, Thermal deployment

Object creation process : Allocate memory space + The default value is + initialization , Class loading process : Load class + The default value is + initialization

DCL

volatile( visibility 、 Orderliness ): Prevent other threads from reading dirty data due to instruction rearrangement

Hardware layer data consistency

 Insert picture description here

WCBuffer:write combine buffer Merge write

Problems arise

  1. Lock bus ( When the data in memory is cpu In operation , other cpu Temporarily unable to access memory again )
  2. Agreement of conformity ( Add different tags through different states )
    1. MESI:
      1. modified: Be modified
      2. exclusive: Only one cpu Read
      3. share: Multiple cpu Read at the same time
      4. invalid: When one cpu When reading data, another cpu Modifying data

Basic unit

cpu The basic unit for reading data from memory :cache line -> 64bytes, This may lead to sharing problems

  1. Problem description : When two adjacent cells store two values , There are two cpu Want to manipulate these two values , But because each read is a cache line It will cause another irrelevant value to be read in , At this time, when modifying a value , Will lead to another cpu The operation of another value is invalid ( False sharing )
  2. Problem solving : Space for time ( Define multiple Long Type variables populate cache lines )
public class Demo1 {
    
    public volatile long m = 0L; //  Unfilled cache line Time is 2000 about 
    public volatile long m = 0L,l1,l2,l3,l4,l5,l6; //  fill cache line(cache line padding)  Time is 800 about 
    //  In filling cache line Every time after cpu Read one cache line
    //  In unfilled cache line Before , Each read d[0] when , because d[0] The size of is less than 64byte So I will d[1] Read in , So when one is distributed in different cpu Threads of access different d The elements of 
     It will lead to data consistency problems ( Lock bus 、 Agreement of conformity ), Will reduce performance 
    //  In filling cache line after , Each read d[0] when , because d[0] The size of is close to 64byte Then only the current element will be read and operated , So when located in other cpu Read by other threads d[1] Conduct 
         Operation does not affect each other , Improve performance 
    public static Demo1[] d = new Demo1[2];
    static{
    
        d[0] = new Demo1();
        d[1] = new Demo1();
    }
    public static void main(String[] args) throws InterruptedException {
    
        Thread t1 = new Thread(()->{
    
            for (int i = 0; i < 10000_0000L; i++) {
    
                d[0].m = i;
            }
        });

        Thread t2 = new Thread(()->{
    
            for (int i = 0; i < 10000_0000L; i++) {
    
                d[1].m = i;
            }
        });

        long start = System.nanoTime();
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long end = System.nanoTime();
        System.out.println((end - start) / 100_0000L);

    }
}

Disorder

  1. Disordered reading , Only without affecting the final result ,cpu All instructions will be optimized and reordered
  2. Merge write (WC buffer:write combining buffer -> 4 Bytes ): In every time cpu When the data is calculated and put into memory, it will pass WCBuffer Temporary data , however WCBuffer Only 4 The size of bytes , If the result of data calculation is less than WCBuffer The size of will be directly put into it and flushed back to memory , Otherwise, it will be filled first WCBuffer And flush back to memory at the same time CPU After the calculation, the remaining data will wait WCBuffer Continue to refresh back to memory after idle , Then there will be performance problems

Order guarantees

Hardware memory barrier

  1. cpu Level ( be based on intel x86):
    1. sfence(store fence): be located sfence The write command before the instruction occurs first after the instruction
    2. lfence(load fence): be located lfence The read command before the instruction occurs first in the read command after the instruction
    3. mfence(storeload fence): be located mfence The read-write command before the instruction occurs first in the read-write command after the instruction
  2. java Assembly level :lock Instructions

jvm standard

Specific implementation based on hardware

  1. loadload
  2. storestore
  3. storeload
  4. loadstore

volatile Realization

Bytecode

acc_volatile

jvm

Add a barrier before and after the command

os

win

lock

Linux

barrier +lock

synchrinized Realization

Bytecode

Method

acc_synchronized

Code block

moniterenter moniterexit

jvm

c c++ Call the underlying synchronization mechanism of the operating system

OS

lock comxchg xxx

Object memory layout

Object creation process

  1. class loading
  2. class linking(verification prepare resolution)
  3. class initialization
  4. Application memory
  5. Object attributes are given default values
  6. Call constructor
    1. Object property initialization
    2. Call constructor

The composition of objects

Common object

  1. Object head :markword 8
  2. classpointer: Class pointer , Point to the class
  3. The instance data
  4. padding: The space occupied by filling objects is 8 Integer multiple

Array objects

  1. Object head :markword 8
  2. classpointer: Class pointer , Point to the class
  3. Length of array
  4. Array data
  5. padding: The space occupied by filling objects is 8 Integer multiple

Size of object

  1. Object head :8byte
  2. classpointer: primary 8 byte ,jvm Default on UseCompressClassPointer after 4byte
  3. Oops:Ordinary object pointer size, The default is 8 Bytes , By using :XX:+UseCompressOops After compression, it is half
  4. data size
  5. fill 8 Integer multiple

Composition of object header

 Insert picture description here

  1. lock : Lock or not 2bit
  2. Biased locking :1bit
  3. Generational age :4bit
  4. Once an object is calculated identityhashcode You cannot enter the locked state

Object positioning

  1. Handle to the pool
  2. Direct to

JMM

Basic division

 Insert picture description here

Method area

    1. 8 Before : Forever permanent gen,fgc No recovery
  • 1.8 after : Meta space meta space

jvm Stack ( The sub elements are stack frames )

Local variable table

Include parameter list 、 Custom parameters 、this( Example method )

The stack of operands

Record the value of the operation

Dynamic links

Implement value reference conversion when checking that there is a symbol reference that is converted to a value reference

Method return address

Record the address to which the returned value should be returned

Interview questions

Code 1:

public class Demo2 {
    

    public static void main(String[] args) {
    
        
        int i = 8;
        i = i++; // i++ It is the self increment of the variable value on the local variable table , and i = xx, Is to pop up the value at the top of the operand stack and assign it to i, This is why the result is still 8
        System.out.println(i); //  The result is 8
    }
}

Bytecode 1:

 0 bipush 8  //  Like putting values in the operand stack 
 2 istore_1	 //  Pop up the value at the top of the operand stack and assign it to the local variable table with the subscript 1 On variables of 
 3 iload_1   //  Subscript the local variable table as 1 The value of the variable of is pushed into the operand stack 
 4 iinc 1 by 1  //  Subscript the local variable table as 1 Add one to the value of the variable of 
 7 istore_1  //  Pop up the value at the top of the operand stack and assign it to the local variable table with the subscript 1 On variables of 
 8 getstatic #2 <java/lang/System.out : Ljava/io/PrintStream;>
11 iload_1
12 invokevirtual #3 <java/io/PrintStream.println : (I)V>
15 return

Code 2:

public class Demo2 {
    

    public static void main(String[] args) {
    
        
        int i = 8;
        i = ++i; // ++i It is the self increment of the variable value on the local variable table , and i = xx, Is to pop up the value at the top of the operand stack and assign it to i, This is why the result is 9
        System.out.println(i); //  The result is 8
    }
}

Bytecode 2:

 0 bipush 8  //  take 8 Into the stack of operands 
 2 istore_1 //  Subscript the local variable table as 1 The variable of gives the value of the top of the operand stack 
 3 iinc 1 by 1  //  Subscript the local variable table as 1 The value of the variable plus 1
 6 iload_1 //  The following table in the local variable table is 1 The value of the variable of is put into the operand stack 
 7 istore_1 //  Pop up the value of the element at the top of the operand stack and assign it to the local variable table. The following table is 1 Value 
 8 getstatic #2 <java/lang/System.out : Ljava/io/PrintStream;>
11 iload_1
12 invokevirtual #3 <java/io/PrintStream.println : (I)V>
15 return

jvm Instruction set

invoke series

invoke_static

Call static methods

invoke_virtual

Call methods that support polymorphism ( Include final Examples of embellishments )

invoke_special

call private Method or construction method

invoke_dynamic

adopt lambda Expression related methods . Be careful :() -> {} Will create a in the current class Lambda Inner class and create an inner class again in this inner class

The garbage

Definition

No reference to the object

The way of judging

Quoting Technology (python)

The circular reference problem cannot be solved

The root can reach

Can be used as the type of root :

  1. Thread stack variables : One method calls another
  2. Static variables
  3. Constant pool
  4. JNI The pointer (java native interface)

Garbage collection algorithm

Mark —— eliminate (mark sweep)

Scan first and then remove the garbage
It is applicable to the situation where there are many surviving objects
Need to traverse twice , Low efficiency , And will produce a lot of space debris

Copy (copy)—— The new generation

Divide the space into two pieces , First traverse to determine which objects are not garbage , Then copy to another space , Then clear the whole space
You need to traverse it once , More efficient , But it will change the address of the object in memory , There is a little trouble in the follow-up

Mark —— Compress (mark compact)

Gather all objects that cannot be cleared
Two iterations , Low efficiency , It is better to

Generational garbage collection algorithms

except Epsilon ZGC Shenandoah All the others use logical generational models
G1 Use logical generation , Physics has no generations
In addition, they are all logical generations 、 Physical generation

  • The new generation : Copy algorithm
  • Old age : Clearly marked 、 Tag compression

Logical allocation of heap space

Old age : The new generation =2:1
eden:survivor=8:2
 Insert picture description here

Method area (method area)

Forever perm gen(1.8 Before )

Meta space meta space(1.8 after )

Object on the stack

On the stack

Small objects
No escape analysis
Scalar substitution

Thread local cache TLAB(thread local allocate buffer)

Forever

eden

When does the subject enter the old age

  1. When you are older than XX:MaxTenuringThreshold Put it in the old generation
  2. Dynamic allocation : When survivor When the total age of the surviving objects exceeds half, they will enter the elderly generation
  3. Distribution guarantee : When survivor When there is not enough space , Space guarantee allocation enters the elderly generation

Common garbage collectors

 Insert picture description here

serial

Single thread call recycling ,stop-the-world

serialOld

Single thread recycling ,stop-the-world

Parallel Scavenge

Concurrent recovery ,stop-the-world

Parallel Old

Concurrent recovery ,stop-the-world

ParallelNew

be based on parallel scavenge enhance , In combination with CMS

CMS(concurrent mark sweep)

Concurrent tags 、 Clear away rubbish

The process

  1. Initial marker
  2. Concurrent Tags
  3. Re label
  4. Concurrent cleanup ( Mark clear )

problem

  1. memory fragment( Using tags —— Compression algorithm )
    1. -XX:+UseCompactAtFullCollection -XX:+CMSGCsBeforeCompaction
  2. floating garbage( During concurrent cleanup )
    1. Concurrent Mode Failure
      1. Lower the departure CMS The threshold of :-XX:CMSInitiatingOccupancyFraction Default 92%
    2. PromotionFaild
      1. The solution is similar to , Keep enough space for the elderly

G1

1.8 mature ,1.9 Default
Use tricolor markers

ZGC

Shenandoah

Epsilon

Use GC

  1. -XX:+UseSerialGC => Serial + SerialOld
  2. -XX:+UseParallelGC => ParallelScavenge + SerialOld (ParallelScavenge +ParallelOld 1.8 Default )
  3. -XX:+UseParallelOldGC => Parallel Scavenge + Parallel Old
  4. -XX:+UseConcMarkSweepGC => ParaNew + CMS + SerialOld( When cms When there is too much debris space in the implementation process, it will be used SerialOld)
  5. -XX:+UseG1GC

JVM tuning

Command Introduction

classification

  • -xxxxx: Represents a standard command
  • -x:xxx: Indicates the command used in a specific version
  • -xx:xxx: Indicates instability , The next version may not support

Basic concepts

  1. throughput : User program execution time / ( User program execution time + Garbage collection time )
  2. response time :STW,stw The less response time, the higher
  3. Pursue throughput :Parallel Scavenger + Parallel + Old, Pursue response time :(1.8)G1、ParallelNew + CMS, You can also improve throughput when the response time meets

What is tuning

  1. According to the needs jvm Planning and pre tuning
  2. Optimize operation jvm Environmental Science
  3. solve jvm All kinds of problems in the process of operation (OOM)

Problem solving

  1. CPU Utilization rate reaches 100%
    1. Through the first top Check the occupation of that process cpu The most resources top
    2. Find the thread in the process CPU The most resources top -Hp
    3. adopt jstack Save the stack of threads
    4. Finding out which method is more time-consuming jstack
  2. CPU Memory usage is soaring
    1. Export heap memory (jmap)
    2. analysis (jprofiler、jstat、jvisualvm..)
  3. monitor CPU
    1. jprofiler、jstat、jvisualvm

Specifically

  1. View object creation (OOM)
    1. jmap => OOM
      1. -histo Check how many objects are created
      2. -dump Create dump file , Generally, the memory is too big, and there is a jam , When there is a polymorphic server backup , have access to
    2. The graphical interface : Generally used for testing (jconsole、jvisualvm、jprofiler)
    3. arthas Online monitoring
  2. Check thread operation
    1. jstack

command

-XX:+PrintCommandLineFlags
-XX:+PrintContainerInfo
-XX:+TraceClassLoading
-XX:+TraceClassResolution
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=path
-XX:+DisableExplicitGC
-XX:+PrintGC
-XX:+UseConcMarkSweepGC
-XX:+UseG1GC
-XX:+UseParallelGC
-XX:+UseParallelOldGC
-XX:+UseParNewGC
-XX:+UseSerialGC
-XX:+PrintFlagsFinal

Concurrent recycling algorithm

G1

Tri-colour marking ( Concurrent Tags )

There are different states for objects

  1. Concept
    1. black : Indicates that the object itself and the attributes within the object are marked as non garbage
    2. gray : Indicates that the object itself is marked as non garbage, But the attributes in the object are not marked as non garbage
    3. white : Indicates that the object is not marked as non garbage
  2. problem :
    1. Missing mark : Indicates that during the marking process, the reference that originally points to a white object suddenly disappears , And there is a black object pointing to this object
      1. Necessary condition : The current white object is pointed by a black marker , The reference that was originally associated with the gray object disappears
      2. problem : Because the black object has been marked , So the object pointed by the black object will not be scanned again when re marking , So the white object will be regarded as garbage It's recycled ( No reference points to )
      3. resolvent :
        1. increment update(CMS): Record the added references , And modify the color of the current object , So when you mark again, you will find the white object again , And mark , It won't be recycled , This method will scan the whole heap to find the direction of the original black object
        2. SATB(snapshot at the begining)(G1): Record deleted references , And put it on its own stack , combination CSet, In the process of re marking, the record in the stack of the object will be scanned and the white object will be found , White objects are based on their own Rset Find a black object pointing to yourself , So it will be re marked and will not be recycled
        3. Why? G1 Use SATB: because g1 The implementation of is to divide the heap into different region, therefore , To reduce scanning all region, So I'll use satb combination CSet You only need to scan the objects related to yourself

Color pointer

card table

When doing fgc You need to scan the whole old District , More time-consuming , Therefore, based on bitmap Of cardtable It was born , If old There are objects pointing to y The area will be marked dirtycard, When scanning, you only need to scan cardtable that will do

CSet(collection set)

Records other object references that the object points to

Rset(Remember Set)

Records all other object references to this object

fibers

Threads vs fibers

  1. One in kernel mode 、 One is in user mode
  2. A lightweight 、 A heavyweight
原网站

版权声明
本文为[Jonny Jiang-zh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130733240545.html