当前位置:网站首页>JVM memory structure
JVM memory structure
2022-07-29 06:26:00 【lalajh】
problem :
be-all Java Developers may encounter such confusion ? How much space should I set for heap memory ?OutOfMemoryError Which area of runtime data is involved in the exception of ? How to solve it ? In fact, if you often solve server performance problems , Then these problems will become very common , understand JVM Memory is also used to quickly understand the problem of the memory area when the server has performance problems , In order to solve production problems quickly .
chart :

1、 Method area
Definition : Method area , Also called non heap (Non-Heap), Is a memory area shared by threads . It mainly stores the loaded class bytecode 、class/method/field And so on 、static-final Constant 、static Variable 、JIT Compiler compiled code and other data . in addition , The method area contains a special area “ Runtime constant pool ”.
1.1 Data stored in the method area
All types are limited to names : Full name package Path combined with class name ;
The fully qualified name of the direct superclass of type : The fully qualified name of the parent class or superclass ;
Is the type a class type or an interface type : Determine whether the current class is Class Or interface Interface;
Type of access modifier : Judge modifier , Such as pulic,private etc. ;
Constant pool of type :;
Field information : Information of fields in class ;
Methods information : Information about methods in the class ;
Static variables : Static variable information in class ;
One to class ClassLoader References to : Yes ClassLoader References to , This reference points to heap memory ;
One to Class Class reference : References to object instances , This reference points to heap memory .
As you can see from the diagram , The method area stores ClassLoader References to objects , It also stores a reference to the class object , These two referenced object instances will be stored in heap memory .
1.2 Runtime constant pool

stay Class File structure , The first 4 Bytes for Storage Megic Number, Used to determine whether a file can be JVM Accept , We'll go on with the 4 Bytes for version number , front 2 Bytes Store the minor version number , after 2 individual Store the major version number , Then there is the... For storing constants Constant pool , Because the number of constants is not fixed , So the entrance to the constant pool places a u2 Data of type (constant_pool_count) Store constant pool capacity meter values .
Constant pool is mainly used to store two kinds of constants : Literal (Literal) And symbol references (Symbolic References).
Actually Class The relationship between the constant pool in the file and the runtime constant pool is very easy to understand ,Class The constant pool in the file is used to store various literal quantities and symbol references generated during compilation , This part will be stored in the runtime constant pool of the method area after the class is loaded .
In summary , Compile time uses Class Constant pool in file , Runtime uses the runtime constant pool .
Runtime constant pool relative to Class Another important feature of file constant pool is its dynamic nature ,Java Languages do not require constants to be generated only at compile time , It's not preset Class The contents of the constant pool in the file can enter the method area runtime constant pool , It is also possible to pool new constants during runtime , This feature is used more by developers String Class intern() Method .
Advantages of constant pool
Avoid frequent creation and destruction of objects that may affect system performance , Realize the sharing of objects . For example, string constant pool , Put all string literals in a constant pool at compile time .
Save memory space : All the same string constants in the constant pool are merged , Take up only one space .
Save running time : When comparing strings ,== Than equals () fast . For two reference variables , Only == Determines whether the references are equal , So you can tell if the actual values are equal .
1.3 Method area memory changes
2、JVM Heap memory
2.1 What is heap memory
Definition : Heap memory is JVM Startup time , A piece of memory space obtained from the operating system , It is mainly used to store the instance object itself , The created object will be placed in heap memory .
Physical level : From the physical level ( Hardware level ) Come on , When Java When the program starts to run ,JVM Will get some memory from the operating system , Part of that memory is heap memory .
Development level : At the development level , Heap memory is usually at the bottom of the storage address , Line up . When an object passes through new Keywords or created by other means , Object gets memory from the heap . When the object is no longer used , After being recycled as garbage , This memory goes back to heap memory .
2.2 Heap memory structure
Heap memory is structurally , Divided into younger generations (YoungGen) And the older generation (OldGen) Two parts ;
The younger generation (YoungGen) It can also be divided into generating areas (Eden) And the survivors area (Survivor) Two parts ;
Survivors area (Survivor) It can be subdivided into S0 District (from space) and S1 District (to space) Two parts .

characteristic
1.JVM Memory is divided into heap memory and non heap memory , Heap memory is divided into younger generations (YoungGen)、 Old age (OldGen);
2.Eden The area occupies a large capacity ,Survivor Two zones take up a small capacity , The default scale is 8:1:1;
3. Heap memory holds objects
2.3 Generation concept of heap memory

generational : The heap memory is divided into modules from the conceptual level , The whole is divided into two parts , The young and the old . Divide the heap memory into memory capacity from the physical level , Part of it will be distributed to the younger generation , Part of it is distributed to the elderly . This is what we call generational .
The meaning of generations :
1. Easy heap memory classification management , Easy garbage collection : Similar to what we often use Windows operating system , We will set aside part of the storage space of the physical disk as the installation disk of the user system ( Such as C disc ), We are also most likely to divide the remaining disk space into C, D, E Wait for disk , Used to store the same type of data .
2. Easier to manage : The same is true for the generation of heap space , For example, newly created objects will enter the younger generation (YoungGen) Generation area of (Eden), Reachable objects whose life cycle is not over , After many garbage collections , Will be stored in the elderly (OldGen), This is classified management ;
3. Easy garbage collection : Classify objects according to their survival probability , For long-lived objects , Put it in a fixed area , So as to reduce the scanning time and GC frequency . Different garbage collection algorithms for classification , Make full use of the advantages and avoid the disadvantages of the algorithm .
3、 JVM Object transfer and age judgment in the heap
3.1 Object transfer

As can be seen from the above figure : The newly generated non large objects are first put into the younger generation Eden District , When Eden The space is full , Trigger Minor GC, The surviving objects move to Survivor0 District ,Survivor0 Trigger execution when the zone is full Minor GC,Survivor0 Live objects move to Suvivor1 District , This ensures that there will always be a survivor The area is empty . After many times Minor GC Objects that are still alive move to the old age .
If the newly generated object is a large object , The object will be directly stored in the elderly .
The older generation stores long-lived objects ,GC All threads will be stopped waiting GC complete , Therefore, the application with high response requirements should minimize the occurrence of Major GC, Avoid response timeouts .
What is a big object : 10M Is the object big ?100M The object of ? What is a big object ? The standard of large objects can be defined by developers , our JVM Parameters in , Can pass -XX:PretenureSizeThreshold This parameter sets the standard for large objects , Unfortunately, this parameter is only correct Serial and ParNew Two new generation collectors work .
Cannot set -XX:PretenureSizeThreshold Parametric JVM Come on , Eden Objects with insufficient storage capacity are so-called large objects .
3.2 Age judgment
effect ·:JVM Judge whether the object should be stored in the elderly generation by judging the specific age of the object ,JVM Through the judgment of age, we can complete the transfer of objects from the young generation to the old age .
Target age (Age) Counter :HotSpot Most collectors in virtual machines use generational collection to manage heap memory , Then memory recycling must be able to decide which surviving objects should be placed in the new generation , Which living objects are in the old generation . To do this , The virtual machine defines an object age for each object (Age) Counter , Stored in the object header .
Increase in age : The object is usually in Eden The district was born , If you go through the first time Minor GC Still alive after , And can be Survivor If it can be accommodated , The object will be moved to Survivor In the space , And set its target age to 1 year . The object is Survivor Every time I get through the area Minor GC, Age increases 1 year .
Age default threshold : When its age increases to a certain extent ( The default is 15), Will be promoted to the senior generation . The age threshold of the age at which the target is promoted to the old age , You can use the parameter -XX:MaxTenuringThreshold Set up .
————————————————
Reference resources :
https://blog.csdn.net/a321123b/article/details/123329127
边栏推荐
- [beauty of software engineering - column notes] 19 | as a programmer, you should have product awareness
- MySql-面试题
- 官方教程 Redshift 08 Light
- 多线程和并发
- Unity初学1——角色移动控制(2d)
- Operating system interview questions
- [beauty of software engineering - column notes] 14 | project management tools: all management problems should be considered whether they can be solved by tools
- Markdown and typora
- 八大排序-----------快速排序
- c语言面试准备一(谈谈理解系类)
猜你喜欢

Add time series index to two-dimensional table
![寒假集训总结 (1.23~1.28) [第一梯队]](/img/cf/2f86ecc23bfe6d96ad0429c785663a.png)
寒假集训总结 (1.23~1.28) [第一梯队]

LeetCode #83. 删除排序链表中的重复元素

Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图

官方教程 Redshift 05 system参数详细解释

Leetcode notes 452. minimum number of arrows to burst balloons (medium) 452. detonate balloons with the minimum number of arrows (medium)

官方教程 Redshift 04 渲染参数

UE5 光影基础 阴影全解析 锯齿阴影解决方案 Lumen

Operating system interview questions

计算机大厂面试题
随机推荐
LeetCode #14. 最长公共前缀
Ue5 texture system explanation and common problem setting and Solutions
Access、Hybrid和Trunk三种模式的理解
Leetcode 1. sum of two numbers
Leetcode 7. integer inversion
Official tutorial redshift 03 parameters and general instructions of various GI
NOI Online 2022普及组 题解&个人领悟
链表--------------------尾插法
Leetcode notes 605. can place flowers (easy) 605. planting flowers
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
Add time series index to two-dimensional table
synchronized八锁现象理解
官方教程 Redshift 03 各种GI的参数和常规使用说明
Ml self study notes 5
赛博朋克版特效shader
leetcode---技巧
LeetCode #7.整数反转
Leetcode 9. palindromes
Encapsulation - Super keyword
Leetcode 283. move zero