当前位置:网站首页>JVM memory and garbage collection-3-runtime data area / method area
JVM memory and garbage collection-3-runtime data area / method area
2022-07-08 01:58:00 【xcrj】
summary
- The method area is logically continuous , Physically dispersible
- Object references are in the stack (LV in ), The object entity is in the heap , The object class information is in the method area
OOM,JVM Too many classes loaded - jdk7-
java.lang.OutOfMemoryError: PermGen space
,jdk8=+ava.lang.OutOfMemoryError: Metaspace
- Load third party jar There are too many classes in the package ;tomcat Too many projects deployed ; There are too many dynamically generated reflection classes
Forever / Meta space
Introduce
-jdk8 Used meta space , Abandoned the permanent generation
- Metaspace uses local memory , Permanent generation uses virtual machine memory
Forever (jdk7-)
-XX:PermSize
initial min Permanent generation size , The default is 20M+-XX:MaxPermSize
max Permanent generation size ,32 Bit machines default to 60M+,64 Bit machine is the default 80M+- JVM The class information loaded exceeds
-XX:MaxPermSize
Will reportjava.lang.OutOfMemoryError: PermGen space
- Capacity expansion : Permanent memory is allocated first
-XX:PermSize
( Permanent generation of initial memory )》 Not enough 》 Expand the capacity until-XX:MaxPermSize
( Meta space maximum memory ) - Shrinkage capacity : Permanent generation memory after expansion 》 There are idle 》 Shrink until
-XX:PermSize
( Permanent generation of initial memory )
Meta space (jdk8+)
-XX:MetaspaceSize
Initial Metaspace size , The default is 20M+-XX:MaxMetaspaceSize
max Metaspace size , The default is -1, There is no limit- Default
-XX:MaxMetaspaceSize=-1
Under the circumstances , The virtual machine runs out of all available system memory , newspaperjava.lang.OutOfMemoryError: Metaspace
- Different from heap and permanent generation min size , Heap is the concept of warning line
-XX:MetaspaceSize
It's the warning water line 》 Reach this warning water level 》 Just call FGC(Full GC) Recycle useless classes , Free meta space 》 Then reset “ Warning water level ”- Reset “ Warning water level ”:FGC Too little free space To improve properly
-XX:MetaspaceSize
It's the warning water line , Less than-XX:MaxMetaspaceSize
- Reset “ Warning water level ”:FGC Too much free space Appropriate reduction
-XX:MetaspaceSize
It's the warning water line
Memory leaks and memory overflows
Memory leak
Definition : You cannot recycle unwanted objects , Cause memory leaks ; A memory leak has no major impact ; The consequence of memory leak accumulation is memory overflow .
out of memory
Definition : Memory required by the program > The system allocates memory
Handle
The process :dump》eclipse memory analyzer》 Distinguish memory leaks / overflow
if - Memory leak : Check the leak object to GC Roots Reference chain of
if - out of memory : Optimize the code , Reduce the survival time of objects ; Check the heap parameters -Xms and -Xmx
Is it the right size
Method area
Content
- JIT Code cache :
- Cache machine language ( Translation of hotspot bytecode into machine language )
- Class information :
- Type information
- Methods information
- Domain information (Field Information )
- Runtime constant pool :
- contain String constant ( Different versions jdk Different positions of string constants )
- Static variables
Class information / Type information
Class head
- Access modifier
- Class full name ( Package name . Class name )
- Immediate parents ( Full name ,interface and java.lang.Object No parent )
- List of direct interfaces (java Multiple interfaces can be implemented )
Class information / Domain information
Domain header
- Access modifier
- type
- Domain name
Class information / Methods information
Method head
- Access modifier
- Return type
- Method name
- Input parameter type , Number , The order
Methods the internal
- Method bytecode
- LV(abstract and native There is no way LV)
- OS
- Anomaly table
Static constant pool
brief introduction
- constant pool table yes
*class
Part of the document , Store the literal and symbol references generated after compilation - constant pool table After being loaded by the class, it is stored in the runtime constant pool in the method area
- javac compile
*.java
After the document*.class
Constant pool in file - Will be public , Take out the things that may be used , Reduce
*.class
file size ,*.class
Store the reference of the required symbol in the file - Static constant pools are not dynamic
Content
- Literal ( String constant ,final Constant etc. )
- Symbol reference ( type - Class head , Method - Method head , Domain - Domain header )
Runtime constant pool
- be located
*.class
The static constant pool in the file is loaded by the class and stored in the runtime constant pool in the method area - When the class loading , Reference the symbols required by the class ( In the static constant pool ) Turn to the real address ( In the runtime constant pool )
- Every class or interface maintains 1 Runtime constant pool , Constants in the runtime constant pool are accessed through real addresses
- The runtime constant pool is dynamic , You can add new constants to it , for example
String.intern()
Method Add string constants to the runtime constant pool
evolution
analysis
- jdk7=+ Static variables and string constant pools are put on the heap , Because the method area does not recycle often , Static variables and string constants are often created and used
Forever To Meta space :
- Permanent generation size is difficult to determine , Tuning is difficult
- Permanent generation garbage collection is complex
Garbage collection
brief introduction
- Garbage collection in the method area is difficult , In particular, the type of uninstall
- Mainly recycling “ Obsolete constants in the runtime constant pool ” and “ Types that are no longer used ”
- Java The virtual machine specification does not mandate garbage collection in the method area
- FGC: Support garbage collection in the method area
- ZGC(jdk11): Garbage collection in the method area is not supported
- In the extensive use of bytecode ( Reflection , A dynamic proxy ,CGLib Equal bytecode framework ) And custom class loader ( Dynamic generation JSP,OSGI) Generally need JVM It has the ability to unload classes
Method area content
- Literal ( String constant ,final Constant etc. )
- Symbol reference ( type - Class head , Method - Method head , Domain - Domain header )
Recycling - Abandoned constants
- Constants in the constant pool can be recycled as long as they are not referenced anywhere
- Similar to recycling java Obsolete objects in the heap
Recycling - Symbol reference / type
- There are no instances of this class and related derived subclasses in the heap && Of the class java.lang.Class The object is not referenced anywhere && The class loader that loads this class has been recycled .
- It is difficult to achieve the above three conditions , At the same time meet the above 3 Only three conditions are allowed to be recycled
example
How many classes are loaded
Code
package xcrj;
/* * Customize 1 How many classes will be loaded into each class * */
public class VMFunClassNum {
public static void main(String[] args) {
System.out.println("start...");
try {
Thread.sleep(1000000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
command
# compile
javac -d D:\workspace\idea\JVMDemo\blog\target\classes\ -encoding UTF-8 VMFunClassNum.java
# function
java -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMFunClassNum
result
Introduce
- Discovery only defines 1 In the case of two classes , Still loaded in the method area 1612 Classes
Method area OOM
Code
package xcrj;
import com.sun.xml.internal.ws.org.objectweb.asm.ClassWriter;
import com.sun.xml.internal.ws.org.objectweb.asm.Opcodes;
/* * extends ClassLoader * First set up -XX:MetaspaceSize10m -XX:MaxMetaspaceSize10m * */
public class VMMethodOOM extends ClassLoader {
public static void main(String[] args) {
int count = 0;
try {
VMMethodOOM methodOOMTest = new VMMethodOOM();
for (int i = 0; i < 10000; i++) {
// establish ClassWriter object , Bytecode for production classes
ClassWriter classWriter = new ClassWriter(0);
// Parameters from left to right : Version number 1.8, Access control , Class name , Package name , Parent class , Interface
classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, "Class" + i, null, "java/lang/Object", null);
//byte[]
byte[] code = classWriter.toByteArray();
// Load bytecode , Generate Class The object is in the method area ; Such kind extends ClassLoader
methodOOMTest.defineClass("Class" + i, code, 0, code.length);
count++;
}
} finally {
// Do not catch exceptions , Whether it is abnormal or not, it will execute finally The code in
System.out.println(" Number of generated classes :" + count);
}
}
}
command
# compile
# -XDignore.symbol.file=true, close javac The default from the “ Symbolic file ” Read class in , Instead of from “rt.jar” Read class in
javac -XDignore.symbol.file=true -d D:\workspace\idea\JVMDemo\blog\target\classes\ -encoding UTF-8 VMMethodOOM.java
# function
java -XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ xcrj.VMMethodOOM
result
Static constant pool
# Numbers
Is to link to the constant pool
tuning
Parameters
classification | Parameters | effect | Suggest |
---|---|---|---|
jdk7=-, Forever | -XX:PermSize=100m | Initial permanent generation size | no |
jdk7=-, Forever | -XX:MaxPermSize=100m | max Permanent generation size | no |
jdk8=+, Meta space | -XX:MetaspaceSize=100m | Initial Metaspace size , cordon , Set a large value to prevent insufficient space FGC | yes |
jdk8=+, Meta space | -XX:MaxMetaspaceSize=100m | Maximum Metaspace size , Default -1 | no |
边栏推荐
- 很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
- Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
- Voice of users | winter goes and spring comes, waiting for flowers to bloom -- on gbase 8A learning comprehension
- The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
- How to fix the slip ring
- From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
- Tencent game client development interview (unity + cocos) double bombing social recruitment 6 rounds of interviews
- 分布式定时任务之XXL-JOB
- Uniapp one click Copy function effect demo (finishing)
- Why did MySQL query not go to the index? This article will give you a comprehensive analysis
猜你喜欢
The function of carbon brush slip ring in generator
《ClickHouse原理解析与应用实践》读书笔记(7)
QT -- create QT program
ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版
Redux usage
云原生应用开发之 gRPC 入门
C语言-Cmake-CMakeLists.txt教程
Reading notes of Clickhouse principle analysis and Application Practice (7)
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
随机推荐
Redisson分布式锁解锁异常
【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking
用户之声 | 对于GBase 8a数据库学习的感悟
快速熟知XML解析
Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
Mouse event - event object
C语言-Cmake-CMakeLists.txt教程
Version 2.0 of tapdata, the open source live data platform, has been released
电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
How mysql/mariadb generates core files
Codeforces Round #649 (Div. 2)——A. XXXXX
神经网络与深度学习-5- 感知机-PyTorch
Summary of log feature selection (based on Tianchi competition)
【目标跟踪】|atom
软件测试笔试题你会吗?
nmap工具介绍及常用命令
Can you write the software test questions?
Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
Codeforces Round #649 (Div. 2)——A. XXXXX
QT -- create QT program