当前位置:网站首页>JVM memory and garbage collection-3-runtime data area / heap area

JVM memory and garbage collection-3-runtime data area / heap area

2022-07-08 01:58:00 xcrj

summary

  • java process :JVM process =1:1;JVM process :runtime example =1:1;runtime example : Heap area =1:1
  • JVM Create and size at startup
  • The heap area must be logically continuous
  • Almost all reference types ( Array , object ) Allocate memory in the heap
  • The heap area is GC Key areas of ,GC Objects in the heap will be recycled

Heap space

 Insert picture description here
analysis

  • 80%+ The objects of eden District death ( transient )

GC

executive summary

 Insert picture description here
analysis

  • Frequently collected in the Cenozoic , Rarely collected in older generations , Almost not in the permanent generation / Meta space collection
classification GC Trigger Introduce
Part of the collection YGC(Young/Minor GC)eden The space is insufficient Collect the new generation
Part of the collection MGC(Major/Old GC) There is not enough space in the old age Collect the old generation , limit CMS GC
Part of the collection Mixed GC Collect the whole Cenozoic and some old ages , limit G1 GC
Full reactor collection Full GC Old age or permanent generation / Lack of meta space The new generation + Old age + Forever / Meta space

YGC(Young/Minor GC)

analysis

  • eden The space is insufficient
  • YGC Recycling the new generation (eden+survivor) Garbage in
  • Recycling speed is very fast
  • STW(stop the world) Stop the user thread , Focus on garbage collection
  • STW Whether it affects the system performance
  • Survivor Zone full doesn't trigger Minor GC
  • -XX:MaxTenuringThreshold=15, Default 15

MGC(Magor/Old GC)

analysis

  • MGC Recycle garbage from the elderly
  • Only CMS GC The elderly generation will be recycled separately

FGC(Full GC)

analysis

  • Old age or permanent generation / Lack of meta space
  • FGC Recycling the new generation + Old age + Forever / Garbage in meta space
  • STW Whether it affects the system performance ,FGC Of STW Time is YGC Of STW The time of the 10 More than times
  • Try to avoid FGC
  • System.gc() System recommendation implementation FGC, Not necessarily

generational GC Algorithm

 Insert picture description here
Special promotion :

  1. eden There's no room for
  • New oversized object ,eden There's no room for , after YGC after ,eden I still can't put it down , In the older generation
  1. survivor There's no room for
  • eden Surviving in the district 1 Large object size >to Zone free space , In the older generation
  • eden The total size of a large number of surviving objects in the area >to Zone free space , In the older generation

Normal promotion :

  • Object survival exceeds the threshold ( Age counter ),-XX:MaxTenuringThreshold=, Default equal to 15, from eden To to Area age counter =1
  • from Area the space occupied by all objects of the same age >from Half of the district

Handling of promotion failure -XX:+HandlePromotionFailure

  • Whether to deal with possible promotion failure , Promotion failure from the new generation to the old age
  • Continuous space for the elderly size> Total number of Cenozoic objects size, It is impossible to fail in promotion
# jdk7=+
if ( Continuous space for the elderly size> Total number of Cenozoic objects size)||( Continuous space for the elderly size> Average historical promotion size){
    Minor GC
}else {
    Full GC
}

# jdk6=-
if ( Continuous space for the elderly size> Total number of Cenozoic objects size)
    Minor GC
}else if (HandlePromotionFailure=true) {
    if ( The largest available continuous space in the old days > History advances to the average size of objects in old times ) {
        Minor GC
    }else {
        Full GC
    }
}else if (HandlePromotionFailure=false) { 
    Full GC
}

Heap size

Heap area

  • Heap size = The new generation + Old age =( Eden + Survival zone )+ Old age =(eden+s0+s1)+ Old age
  • Heap can store the size of objects =(eden+s0/s1)+ Old age . because ,s0 and s1 Replication algorithm used
  • Heap size does not include permanent generations or meta spaces . Permanent generation or meta space is only designed in generational collection algorithm
    Parameters :
  • -Xms( Heap initial memory )
  • -Xmx( Maximum heap memory )

Default :

  • -Xms= Physical memory size /64
  • -Xmx= Physical memory size /4

The process :

  • Capacity expansion : Heap memory is allocated first -Xms( Heap initial memory )》 Not enough 》 Expand the capacity until -Xmx( Maximum heap memory )
  • Shrinkage capacity : Heap memory after expansion 》 There are idle 》 Shrink until -Xms( Heap initial memory )

OOM(OutOfMemoryError):

  • -Xmx Specified memory

Set up :

  • -Xms=-Xmx
  • Purpose : Frequent capacity expansion and shrinkage will affect performance

TLAB(Thread Local Allocation Buffer)

executive summary

  • In the case of multithreading, directly dividing the memory space in the heap area requires locking , Otherwise, the thread is not safe , Locking affects distribution speed
  • JVM Assign to each thread 1 A private cache area TLAB
  • TLAB be located Eden In the space
  • The thread ran out of its own private TLAB Then use it by locking Eden Public areas in

Escape analysis

executive summary

  • HotSpot There is no stack allocation , Implemented scalar substitution
  • jvm With server mode , There is escape analysis ,64 position JVM The default is server Pattern
  • Escape analysis is JIT( Just in time compiler ) One of the optimization techniques in
  • Dynamically analyze the scope of the object , Analyze the scope of use of object references
  • The way to escape : When an object is defined in a method , Pass as a parameter to other methods
  • Thread escape : Such as class variable or instance variable , May be accessed by other threads
  • Escape analysis is “ On the stack ”,“ Synchronous ellipsis ”,“ Scalar substitution ” The basis of
  • Scalar substitution provides a good basis for stack allocation

Scalar substitution

executive summary

  • Replace the object with its member attribute
  • Scalar : The minimum amount of data that cannot be subdivided , The basic type member attribute is scalar
  • The amount of polymerization : The object is the aggregate

On the stack

executive summary

  • after “ Escape analysis ” There is no escape object , It can be allocated directly on the stack
  • No, GC, Because it is the object allocated on the stack

Synchronous ellipsis

executive summary

  • If 1 Objects can only be 1 Thread access , The synchronization operation of this object will be omitted

Decompile

Set the heap size

compile

javac -d D:\workspace\idea\JVMDemo\blog\target\classes\ VMHeapSize.java

function

java -Xmx10m -Xms10m -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapSize

result
 Insert picture description here

  • java virtual vm stay jdk/bin Under the table of contents

result virtual GC
 Insert picture description here

  • The red box adds up to 10M

Object and array location

Code

package xcrj;

public class VMHeapLocation {
    
    public static void main(String[] args) {
    
        VMHeapLocation vmHeap1 = new VMHeapLocation();
        int[] intArr = new int[10];
    }
}

jclasslib
 Insert picture description here

  • Create an object instance :new
  • Create an array instance :newarray

Heap memory size code analysis

Code

package xcrj;

/* *  First set up -Xms500m -Xmx500m * */
public class VMHeapSpaceSize {
    
    public static void main(String[] args) {
    
        // return java The total amount of heap memory in the virtual machine , At the beginning of the totalMemory=-Xms
        long mUnit = 1024 * 1024;
        long initialMemory = Runtime.getRuntime().totalMemory() / mUnit;
        System.out.println("-Xms=" + initialMemory + "M");
        // return java Maximum heap memory the virtual machine is trying to use ,maxMemory=-Xmx
        long maxMemory = Runtime.getRuntime().maxMemory() / mUnit;
        System.out.println("-Xmx=" + maxMemory + "M");

        // 64.0 and 4.0 The purpose is to convert to floating point numbers 
        System.out.println(" The system memory size is : " + initialMemory * 64.0 / 1024 + "G");
        System.out.println(" The system memory size is : " + maxMemory * 4.0 / 1024 + "G");

        try {
    
            Thread.sleep(2000000);
        } catch (InterruptedException ie) {
    
            ie.printStackTrace();
        }
    }
}

result

#  compile 
javac -d D:\workspace\idea\JVMDemo\blog\target\classes\ -encoding UTF-8 VMHeapSpaceSize.java
#  function 
java -Xmx500m -Xms500m -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapSpaceSize

 Insert picture description here
analysis

  • Set up 500m did not , The result of code printing is 479M<500M
  • Heap can store the size of objects =(eden+s0/s1)+ Old age . because ,s0 and s1 Replication algorithm used
  • The memory size of the system does not conform to the actual situation of the author

Heap memory size command analysis

java The command execution is the same as above
Command line view

#  Look at the running java process 
jps
#  see java Process memory 
jstat -gc java process id Number 

 Insert picture description here
analysis

  • E:eden;S:survivor;U:used;C:count;O:old
  • Heap space size = The new generation + Old age =(EC+S0C+S1C)+OC=500M
  • Actual size of heap space =(EC+S0C)+OC=(EC+S1C)+OC=479M

OOM

java

package xcrj;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/* *  First set up  -Xms=10m -Xmx=10m * */
public class VMHeapOOM {
    
    public static void main(String[] args) {
    
        List<Image> list = new ArrayList<>();
        while (true) {
    
            try {
    
                Thread.sleep(10);
            } catch (Exception e) {
    

            }
            int value = new Random().nextInt(1024 * 1024);
            Image img = new Image(value);
            list.add(img);
        }
    }
}

class Image {
    
    private byte[] pixels;

    public Image(int len) {
    
        this.pixels = new byte[len];
    }
}

OOM result
 Insert picture description here

java virsualVM/ A sampler
 Insert picture description here

  • Find out byte[] Objects take up most of the memory

java virsualVM/Visual GC
 Insert picture description here

-XX:NewRatio

Code

package xcrj;

/* *  First set up -Xms500m -Xmx500m * */
public class VMHeapSpaceSize {
    
    public static void main(String[] args) {
    
        // return java The total amount of heap memory in the virtual machine , At the beginning of the totalMemory=-Xms
        long mUnit = 1024 * 1024;
        long initialMemory = Runtime.getRuntime().totalMemory() / mUnit;
        System.out.println("-Xms=" + initialMemory + "M");
        // return java Maximum heap memory the virtual machine is trying to use ,maxMemory=-Xmx
        long maxMemory = Runtime.getRuntime().maxMemory() / mUnit;
        System.out.println("-Xmx=" + maxMemory + "M");

        // 64.0 and 4.0 The purpose is to convert to floating point numbers 
        System.out.println(" The system memory size is : " + initialMemory * 64.0 / 1024 + "G");
        System.out.println(" The system memory size is : " + maxMemory * 4.0 / 1024 + "G");

        try {
    
            Thread.sleep(2000000);
        } catch (InterruptedException ie) {
    
            ie.printStackTrace();
        }
    }
}

jstat result
 Insert picture description here
analysis

  • Old age : The new generation =3
  • OC:(EC+S0C+S1C)=3

jinfo result

#  see JVM Parameter values 
jinfo -flag NewRatio java process id Number 

 Insert picture description here

-XX:SurvivorRatio

analysis

  • Default , With adaptive mechanism

Force to 7:1:1

java -Xmx500m -Xms500m -XX:SurvivorRatio=7 -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapSpace

result
 Insert picture description here

-Xmn100m -XX:NewRatio=3

executive summary

  • -Xmn100m -XX:NewRatio=3 Set together ,-XX:NewRatio=3 invalid
    Instructions
java -Xmx600m -Xms600m -Xmn100m -XX:NewRatio=3 -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapSpaceSize

result
 Insert picture description here

  • Discover the old age : The new generation =OC:(EC+S0C+S1C)!=3:1
  • -XX:NewRatio=3 invalid

-XX:+PrintGCDetails

Code

package xcrj;

import java.util.ArrayList;
import java.util.List;

public class VMHeapGCDetail {
    
    public static void main(String[] args) {
    
        try {
    
            List<String> list = new ArrayList<>();
            String str = "maybe you are a dream";
            do {
    
                list.add(str);
                str += str;
            } while (true);
        // OutOfMemoryError Inherit Error Inherit Throwable
        } catch (Throwable t) {
    
            t.printStackTrace();
        }
    }
}

command

#  compile 
javac -d D:\workspace\idea\JVMDemo\blog\target\classes\ -encoding UTF-8 VMHeapGCDetail.java
#  function 
java -Xmx6m -Xms6m -XX:+PrintGCDetails -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapGCDetail

result
 Insert picture description here

[GC (Allocation Failure) [PSYoungGen: 1008K->498K(1536K)] 1008K->722K(5632K), 0.0526596 secs] [Times: user=0.00 sys=0.00, real=0.05 secs]
[GC (Allocation Failure) [PSYoungGen: 1454K->496K(1536K)] 1678K->1173K(5632K), 0.0017728 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1140K->504K(1536K)] 1817K->1597K(5632K), 0.0012724 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 1356K->0K(1536K)] [ParOldGen: 3589K->3162K(4096K)] 4945K->3162K(5632K), [Metaspace: 2589K->2589K(1056768K)], 0.0287266 secs] [Times: user=0.01 sys=0.00, real=0.03 secs]
[GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] 3162K->3162K(5632K), 0.0021192 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] [ParOldGen: 3162K->3118K(4096K)] 3162K->3118K(5632K), [Metaspace: 2589K->2589K(1056768K)], 0.0096239 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
java.lang.OutOfMemoryError: Java heap space

analysis

  • Throw out OutOfMemoryError Once before Full GC; You can execute once FGC Collected PSYoungGen,ParOldGen and Metaspace Garbage in
  • [GC (Allocation Failure) [PSYoungGen: 1008K->498K(1536K)] 1008K->722K(5632K), 0.0526596 secs] [Times: user=0.00 sys=0.00, real=0.05 secs]:1008K,YGC New generation size before calling ;498K,YGC The size of the new generation remaining after the call ;1536K, The total size of the Cenozoic ;722K,YGC The remaining heap memory size after the call ;5632K, Total heap memory size , about 6M;0.0526596 secs,YGC Time consuming , Company s;Times: user=0.00 sys=0.00, real=0.05 secs Respectively YGC User time consuming 、 The system takes time 、 Actual time taken , The unit is in seconds

eden Super large objects

Code

package xcrj;

/* * -Xms30m -Xmx30m -XX:NewRatio=2 -XX:+PrintGCDetails *  Old age =20M *  The new generation =10M * */
public class VMHeapEdenBIgObj {
    
    public static void main(String[] args) {
    
        //10M Size object 
        byte[] m10Bytes = new byte[1024 * 1024 * 10];
    }
}

result
 Insert picture description here
analysis

  • The new generation =eden+from+to=10240K=10M
  • Old age =20480K=20M
  • Large objects created m10Bytes=10M>eden Of 8192K, In the old days

-XX:+PrintFlagsInitial

result - Default initial value

java -Xms30m -Xmx30m -XX:+PrintFlagsInitial -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapEdenBIgObj

 Insert picture description here
result - Assign the initial value

java -Xms30m -Xmx30m -XX:NewRatio=3 -XX:+PrintFlagsInitial -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapEdenBIgObj

 Insert picture description here

-XX:+PrintFlagsFinal

Instructions

java -Xms30m -Xmx30m -XX:NewRatio=3 -XX:+PrintGCDetails -XX:+PrintFlagsFinal -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapEdenBIgObj

result
 Insert picture description here

HotSpot Scalar substitution

Code

package xcrj;

/* *  Scalar substitution  *  First : Close scalar substitution  * -Xms100m -Xmx100m -XX:+DoEscapeAnalysis -XX:-EliminateAllocations -XX:+PrintGC *  Again : Turn on scalar substitution , The default  * -Xms100m -Xmx100m -XX:+DoEscapeAnalysis -XX:+EliminateAllocations -XX:+PrintGC * */
public class VMHeapScalarReplace {
    
    /* * user There was no escape , Scalar substitution is possible  * */
    public static void createUser() {
    
        User user = new User("xcrj", 20);
    }

    public static void main(String[] args) {
    
        long startTime = System.currentTimeMillis();
        int w200 = 20000000;
        for (int i = 0; i < w200; i++) {
    
            createUser();
        }
        long endTime = System.currentTimeMillis();
        System.out.println(" Spend time :" + (endTime - startTime) + "ms");

        try {
    
            Thread.sleep(1000000);
        } catch (InterruptedException ie) {
    
            ie.printStackTrace();
        }
    }
}

class User {
    
    private String name;
    private int age;

    public User() {
    
    }

    public User(String name, int age) {
    
        this.name = name;
        this.age = age;
    }
}
# jvm With server mode , There is escape analysis ,64 position JVM The default is server Pattern 
java -version
#  Escape analysis is the basis of scalar substitution 
#  Close scalar substitution 
java -Xms2G -Xmx2G -XX:+DoEscapeAnalysis -XX:-EliminateAllocations -XX:+PrintGCDetails -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapScalarReplace
#  Turn on scalar substitution 
java -Xms2G -Xmx2G -XX:+DoEscapeAnalysis -XX:+EliminateAllocations -XX:+PrintGCDetails -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapScalarReplace

result --server Pattern
 Insert picture description here

  • As you can see, yes server Pattern
    result --XX:-EliminateAllocations, Close scalar substitution
     Insert picture description here
  • Created 200w All objects are in the heap
    result --XX:+EliminateAllocations, Turn on scalar substitution , Default on
     Insert picture description here
  • After starting scalar substitution , It is found that there are many fewer objects in the heap , Only 71901 A the

result --XX:PrintGCDetails -XX:-EliminateAllocations, Close scalar substitution
 Insert picture description here

  • YGC Before you call , The new generation used 503809K
    result --XX:PrintGCDetails -XX:+EliminateAllocations, Turn on scalar substitution , Default on
     Insert picture description here
  • YGC Before you call , The new generation used 41984K Far less than 503809K, Scalar substitution is optimized

tuning

Parameters

classification Parameters effect Suggest
Heap space size -Xms10mmin Heap size yes
Heap space size -Xmx10mmax Heap size yes
The size of the new generation -Xmn10m New generation memory size , and -XX:NewRatio=2 Set together , This parameter is invalid no
Old age : The new generation -XX:NewRatio=2 Old age : The new generation =2, Default =2 no
eden:survivor-XX:SurvivorRatio=8eden:s0:s1=8:1:1 no
Cenozoic adaptive -XX:+UseAdaptiveSizePolicy+ No. 1 turns on adaptive ,- Turn off adaptation , Default on yes . Large flow 、 Low delay system Proposed closure
Age counter -XX:MaxTenuringThreshold=15 Age counter , control survivor In the old days , Default =15 no
-XX:+HandlePromotionFailure Open unsafe transfer , Default on no
TLAB Enable -XX:+UseTLAB+ The number is on TLAB, Default on no
TLAB/Eden-XX:TLABWasteTargetPercentTLAB Occupy Eden percentage , Default 1% no
Parameter values -XX:+PrintFlagsInitial Default initial values of all parameters no
Parameter values -XX:+PrintFlagsFinal The final value of all parameters , Assigned value no
GC-XX:+PrintGCDetails Print GC details , Commonly used no
GC-XX:+PrintGC Print GC Brief information no
GC-verbose:gc Print GC Brief information no
Escape analysis -XX:+DoEscapeAnalysis Escape analysis ,jdk6u23=+ Default on no
Escape analysis -XX:+PrintEscapeAnalysis Print escape analysis filter results no
Scalar substitution -XX:+EliminateAllocations Scalar substitution , The default no
JVM Operation mode -serverserver Pattern ,server Escape analysis is only available in mode , Default on no

Instructions

Instructions effect
javac -d D:\workspace\idea\JVMDemo\blog\target\classes\ -encoding UTF-8 VMHeapScalarReplace.java compile
java -Xms100m -Xmx100m -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapScalarReplace function
javap -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ -v xcrj.VMHeapScalarReplace Decompile
jps Look at the running java process
jstat -gc java process id Number see java process GC The memory of
jinfo -flag NewRatio java process id Number see JVM Parameter values
jmap -histo [pid] Check the number of objects in the heap , Take up memory byte, Histogram (histogram)

javap -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ -v xcrj.VMHeapScalarReplace

-Xms=-Xmx

Set up :

  • -Xms=-Xmx
  • Purpose : Frequent capacity expansion and shrinkage will affect performance
原网站

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