当前位置:网站首页>Summary of JVM knowledge points - continuously updated
Summary of JVM knowledge points - continuously updated
2022-04-23 12:55:00 【Xiao an】
Virtual machine specification 11
JVM Memory model
- Thread stack uses stack data structure ,FILO
- Thread stack 、 Method area ( Meta space , The previous permanent generation ) Use direct memory
- The new generation : Old age : Default 1:2
- eden:from:to by 8:1:1 from and to Also known as s0,s1 perhaps s1,s2
- operation a=1 Will be will be 1 Input operation stack , then a Allocate space , Operands stack out
- Generational age ,cms Default 6、 Not cms Default 15( And the largest 15, Because the generation age accounts for 4 byte )
- TLAB The full name is Thread Local Allocation Buffer, That is, the thread allocates the cache locally , This is a thread specific memory allocation area .
Internal structure of stack frame
Each stack frame stores
- Local variable table (Local Variables)
- The stack of operands (Operand Stack) ( Or expression stack )
- Dynamic links (Dynamic Linking) ( Or execute the method reference of the runtime constant pool )
- Method return address (Return Address) ( Or method normal exit or abnormal exit definition )
- Some additional information
To create an object
Object allocation
TLAB Object allocation
Memory allocation policy
- Give priority to Eden District
- Large objects are directly allocated to the old age
Try to avoid too many large objects in the program- Long lived objects are assigned to old age
- Dynamic object age determination
If Survivor The sum of all object sizes of the same age in the area is greater than Survivor Half of the space , Objects older than or equal to this age can enter the elderly generation directly , No need to wait MaxTenuringThreshold The age required in- Space allocation guarantee
-XX:HandlePromotionFailure
Distribution guarantee
Object memory layout
Object access location
Handle access
Direct Pointers
java Object structure
- stay HotSpot In the virtual machine , The layout of objects stored in memory can be divided into 3 area :
Object head (Header)
The instance data (Instance Data)
Alignment filling (Padding).
1. Object head
- markword
The first part markword, Used to store runtime data of the object itself , Such as hash code (HashCode)、GC Generational age 、 Lock status flag 、 A lock held by a thread 、 To the thread ID、 Biased timestamps and so on , The length of this part of data is 32 Bit and 64 Virtual machine of bit ( Compression pointer not turned on ) Respectively 32bit and 64bit, It's officially called “MarkWord”.- klass
The other part of the object header is klass Type a pointer , An object is a pointer to its class metadata , The virtual machine uses this pointer to determine which class instance this object is .- The length of the array ( Only array objects have )
If the object is an array , Then there must be a piece of data in the object header to record the array length .
2. The instance data
- The instance data part is the real effective information stored by the object , It is also the content of various types of fields defined in the program code .
- Whether inherited from the parent class , Defined in subclass , It needs to be recorded .
3. Alignment filling
- The third part alignment filling is not inevitable , It doesn't mean anything , It's just a placeholder .
- because HotSpotVM The automatic memory management system of requires that the object start address must be 8 Integer multiples of bytes , let me put it another way , Is that the size of the object must be 8 Integer multiples of bytes .
- And the header part of the object is exactly 8 Multiple of bytes (1 Times or 2 times ), therefore , When the data part of the object instance is not aligned , It needs to be filled by aligning the padding .
Object size calculation
- stay 32 A system. , Deposit Class It's the size of the pointer 4 byte ,MarkWord yes 4 byte , The object head is 8 byte .
- stay 64 A system. , Deposit Class It's the size of the pointer 8 byte ,MarkWord yes 8 byte , The object head is 16 byte .
- 64 Bit to turn on pointer compression , Deposit Class It's the size of the pointer 4 byte ,MarkWord yes 8 byte , The object head is 12 byte .
The length of the array 4 byte + Array object header 8 byte ( Object reference 4 byte ( Pointer compression is not turned on 64 Position as 8 byte )+ Array markword by 4 byte (64 Bit is not turned on, and the compressed pointer is 8 byte )) + alignment 4 = 16 byte .- Static properties are not included in the object size .
GC Tuning strategy
Strategy 1:
- Reserve new objects for the new generation
because Full GC The cost is much higher than MinorGC, So it's wise to allocate objects to the next generation as much as possible , The actual project is based on GC Log analysis of new generation space size allocation is reasonable- Appropriate adoption “-Xmn” Command to adjust the size of the new generation , Minimize the situation that new objects enter the old age directly .
Strategy 2:
- Big target into the old generation
Although in most cases , It makes sense to assign objects to the next generation . But for large objects, this approach is questionable , If large objects are allocated in the new generation for the first time, there may be insufficient space, which will lead to the old age when many small objects are allocated , Destroy the object structure of the new generation , There may be frequent fullgc. therefore , For large objects , You can set up direct access to the elderly ( Of course, short-lived big objects are a nightmare for garbage collection ).- -XX:PretenureSizeThreshold You can set the object size directly into the old age .
Strategy 3:
- Reasonably set the age of the elderly ,-XX:MaxTenuringThreshold Set the age of the object entering the old age , Reduce the memory footprint of the old age , Reduce full gc Frequency of occurrence .
Strategy 4:
- Set a stable heap size , The heap size setting has two parameters :-Xms Initialize heap size ,-Xmx Maximum heap size .
If the following indicators are met , In general, there is no need for GC Optimize :
- MinorGC Execution time is less than 50ms;
- Minor GC Infrequent execution , about 10 Seconds at a time ;
- Full GC Execution time is less than 1s;
- Full GC Execution frequency is not frequent , No less than 10 minute 1 Time .
Garbage collector algorithm
1. Serial/Serial Old
- Serial It's a replication algorithm ,STW, Serial execution , Garbage collection for the younger generation .
- Serial Old Use the tag - Compression algorithm , Serial execution , Garbage collection in the old days .
2. ParNew and CMS coordination
- ParNew yes Serial Multithreaded version of collector ,STW, Serial execution , Act on the younger generation garbage collection .
3. Parallel Scavenge/Paralled Old(PSPO)
- And ParNew The difference is , for fear of STW Too long , You can set STW The threshold of .
- STW, Serial execution , Using replication algorithm .
- Paralled Old Using tag - Compression algorithm , Garbage collection in the old days ,java1.6 after .
4. CMS(Concurrent Mark Sweep) and ParNew coordination
- In order to reduce the STW Time for , There is CMS The collector , Garbage collection in the old days .
- It is mainly divided into initial marks 、 Concurrent Tags 、 Marking and concurrent cleanup phases again , The foundation is still the mark - Compression algorithm .
- CMS It's based on markers - Clear algorithm , Therefore, after clearing, there are uncontinuous memory fragments , When assigning new objects, we will use FreeList Free list method .
5. G1 The collector
- JAVA8 Then widely used
- G1 Divide the whole pair of areas into several Region, Every Region Its size is 2 Multiple (1M To 32M)
- By setting the size of the heap and Region The quantity is calculated as , most 2000 individual region District , therefore G1 The maximum memory is 32M * 2000region / 1024M = 62.5G
- Region Area division is similar to other collections , The difference is , Separate large objects are assigned to separate region in , A continuous set of Region Area (Humongous start and humonous Contoinue form ), So there are four categories Region(Eden,Survior,Humongous and Old)
- G1 Acts on the entire heap memory area , The purpose of design is to reduce Full GC The birth of
- stay Full GC Because of G1 It's single threaded , There will be a long pause
- G1 Of OldGc The marking process can be similar to yongGc Parallel execution , however OldGc It must be YongGc After performing , namely MixedGc stay yongGC After performing
JVM Tuning parameters
public
- -Xmx10240M
- -Xms10240M
- -XX:MaxMetaspaceSize=512M ( Configuration not recommended )
- -XX:MetaspaceSize=512M
- -XX:+ParallelRefProcEnabled
- -XX:ErrorFile=/export/logs/hs_err_pid%p.log
- -Xloggc:/export/logs/gc-%t.log
- -XX:+UseGCLogFileRotation
- -XX:NumberOfGCLogFiles=5
- -XX:GCLogFileSize=20M
- -XX:HeapDumpPath=/export/logs/
- -XX:+PrintGCDetails
- -XX:+PrintGCDateStamps
- -XX:+HeapDumpOnOutOfMemoryError
- -XX:+PrintCommandLineFlags
G1
- -XX:+UseG1GC
- -XX:MaxGCPauseMillis=100
PSPO
JDK8 Default garbage collector
CMS
Not recommended , It may degenerate into serial gc Garbage collector
- promotion failed Will not lead to fullGC Medium CMS Degenerate to serialGC, because promotion fail after ,youngGC It was stopped , Start fullGC.
- happen concurrent mode failure Can cause Full GC, In this case... Will be used Serial Old The collector , It's single threaded , Yes GC The impact of .
- -XX:+UseConcMarkSweepGC
- -XX:+UseCMSInitiatingOccupancyOnly
- -XX:CMSInitiatingOccupancyFraction=70
- -XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses
- -XX:+CMSClassUnloadingEnabled
- -XX:+ParallelRefProcEnabled
- -XX:+CMSScavengeBeforeRemark
版权声明
本文为[Xiao an]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230615438250.html
边栏推荐
- 【蓝桥杯】4月17日省赛刷题训练(前3道题)
- Record the problems encountered in using v-print
- Kubernets Getting started tutoriel
- Process virtual address space partition
- Stm32cubeprogrammer basic instructions
- BUUCTF WEB [BUUCTF 2018]Online Tool
- BUUCTF WEB [BJDCTF2020]ZJCTF,不过如此
- Kubernetes 入門教程
- 解锁OpenHarmony技术日!年度盛会,即将揭幕!
- Kubernetes 入门教程
猜你喜欢
Fashion cloud learning - input attribute summary
基于卷积神经网络的遥感影像分类识别系统
Record some NPM related problems (messy records)
Zigbee之CC2530最小系统及寄存器配置(1)
Homomorphic encryption technology learning
Labels and paths
梳理網絡IP代理的幾大用途
Number of nodes of complete binary tree
leetcode:437. 路径总和 III【dfs 选还是不选?】
MySQL supports IP access
随机推荐
进程虚拟地址空间区域划分
31. Next arrangement
SSM框架系列——注解开发day2-2
[unity note] basic lighting in l4unity
拥抱机器视觉新蓝海,冀为好望开启数字经济发展新“冀”遇
Importerror after tensorflow installation: DLL load failed: the specified module cannot be found, and the domestic installation is slow
Record the problems encountered in using v-print
SSM框架系列——Junit单元测试优化day2-3
如何实现点击一下物体播放一次动画
【每日一题】棋盘问题
Kubernetes 入門教程
[daily question] chessboard question
Process virtual address space partition
STM32 control stepper motor (ULN2003 + 28byj)
BUUCTF WEB [BJDCTF2020]The mystery of ip
SSL certificate refund instructions
梳理网络IP代理的几大用途
Uni app native app cloud packaging integrated Aurora push (jg-jpush) detailed tutorial
At instruction of nbiot
BUUCTF WEB [BJDCTF2020]ZJCTF,不过如此