当前位置:网站首页>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
analysis
- 80%+ The objects of eden District death ( transient )
GC
executive summary
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
Special promotion :
- 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
- 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
- java virtual vm stay jdk/bin Under the table of contents
result virtual GC
- 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
- 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
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
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
java virsualVM/ A sampler
- Find out byte[] Objects take up most of the memory
java virsualVM/Visual GC
-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
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
-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
-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
- 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
[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
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
result - Assign the initial value
java -Xms30m -Xmx30m -XX:NewRatio=3 -XX:+PrintFlagsInitial -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapEdenBIgObj
-XX:+PrintFlagsFinal
Instructions
java -Xms30m -Xmx30m -XX:NewRatio=3 -XX:+PrintGCDetails -XX:+PrintFlagsFinal -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMHeapEdenBIgObj
result
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
- As you can see, yes server Pattern
result --XX:-EliminateAllocations
, Close scalar substitution - Created 200w All objects are in the heap
result --XX:+EliminateAllocations
, Turn on scalar substitution , Default on - 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
- YGC Before you call , The new generation used 503809K
result --XX:PrintGCDetails -XX:+EliminateAllocations
, Turn on scalar substitution , Default on - 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 | -Xms10m | min Heap size | yes |
Heap space size | -Xmx10m | max 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=8 | eden: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:TLABWasteTargetPercent | TLAB 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 | -server | server 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
边栏推荐
- uniapp一键复制功能效果demo(整理)
- 电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
- How mysql/mariadb generates core files
- Sword finger offer II 041 Average value of sliding window
- Version 2.0 de tapdata, Open Source Live Data Platform est maintenant disponible
- Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
- PHP calculates personal income tax
- [target tracking] |atom
- 批次管控如何实现?MES系统给您答案
- Redismission source code analysis
猜你喜欢
A comprehensive and detailed explanation of static routing configuration, a quick start guide to static routing
Sum of submatrix
微信小程序uniapp页面无法跳转:“navigateTo:fail can not navigateTo a tabbar page“
ClickHouse原理解析与应用实践》读书笔记(8)
C语言-Cmake-CMakeLists.txt教程
系统测试的类型有哪些,我给你介绍
nmap工具介绍及常用命令
List of top ten domestic industrial 3D visual guidance enterprises in 2022
Introduction to ADB tools
保姆级教程:Azkaban执行jar包(带测试样例及结果)
随机推荐
Is NPDP recognized in China? Look at it and you'll see!
VIM string substitution
mysql/mariadb怎样生成core文件
MySQL查询为什么没走索引?这篇文章带你全面解析
什么样的MES系统才是好系统
See how names are added to namespace STD from cmath file
日志特征选择汇总(基于天池比赛)
ClickHouse原理解析与应用实践》读书笔记(8)
【目标跟踪】|atom
How to realize batch control? MES system gives you the answer
云原生应用开发之 gRPC 入门
发现值守设备被攻击后分析思路
Flutter 3.0框架下的小程序运行
Gbase observation | how to protect the security of information system with frequent data leakage
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
Version 2.0 de tapdata, Open Source Live Data Platform est maintenant disponible
[target tracking] |dimp: learning discriminative model prediction for tracking
Can you write the software test questions?
How to make the conductive slip ring signal better
软件测试笔试题你会吗?