当前位置:网站首页>Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
2022-07-07 12:09:00 【Old ox pulling brick】
Let's start with the context , Let's talk about the details . First say jvm How it works , I'll talk about it later jvm The garbage collection of .
java virtual machine abbreviation jvm, It is a fictitious computer , Contains two modules :1. Bytecode instruction set ( assembly language ),2. memory management ( Pile up , Stack , Method area, etc ). At first glance, this name ,java Virtual machine seems to belong to java Virtual machine of language , It's not ,jvm Follow java Language doesn't matter , At least there is no direct relationship .jvm Above is running .class Of documents , Any language contains java As long as the language can be compiled into corresponding .class, So this .class Files can run in jvm above , therefore java It's cross platform . Here's the picture :
The way java From coding to execution : First, compile java Source code *.java, Compiled into *.class file , And then run java *.class command , This time a jvm And it started .jvm Launched the , The first thing to run class Files and java Files needed in the class library , Attention! ,class File loaded into jvm After that, it has to be translated , Translated into assembly language and then handed over to the execution engine , The execution engine calls the server hardware , Start executing logic . Here's the picture :
Let's talk about language translation first : There are two ways to translate :1. compile ,2. explain . The two ways are only different in translation time .
Before a program written in a compiled language is executed , A special compilation process is needed , To compile a program into a machine language file , such as exe file , If you want to run it later, you don't need to translate it again , Just use the results of the compilation (exe file ), Because the translation was done only once , No translation is required at runtime , Therefore, the program execution efficiency of compiled language is high .
The explanation is different , An interpretative language program does not need to be compiled , Save a process , Interpretative languages are translated only when the program is running , For example, explanatory basic Language , There is a special interpreter that can directly execute basic Program , Every statement is translated when it is executed . In this way, the interpretative language must be translated every time it is executed , Low efficiency .
The compilation and interpretation are compared above , Back to jvm Inside :
The text marked in red and bold ,jvm It's also right class Translation of documents , Attention! , Not right java Language for translation , There are two translation tools , Namely : Bytecode interpreter and JIT Just in time compiler .
It's the same thing , Bytecode interpreter Begin to explain and execute sentence by sentence , Pay attention to it, and explain it only when you use it , Explain every sentence you use , I have to explain every time, so the efficiency is relatively low . What if there is hot code , For example, call a method frequently or call a piece of code circularly , Is it always doing repetitive translation , So what to do ?
So we use JIT Just in time compiler , It translates the hot code only once , Save up , Just call it directly when you need it , No need to translate every time .
Say below jvm The garbage collection of
We write programs , During operation , You need to apply for space in memory , If in the process of execution , There will always be programs or data , They have fulfilled their mission , It's no use , White space , At this time, it becomes garbage , If you don't clean up , The space is full , Then the latter procedure cannot be applied .
c and c++ Need to recycle garbage manually , Is to rely on programmers to manage memory garbage , Programmers write their own code to release garbage .c It's direct free,c++ First new, then delete. This will bring about two problems :1. Repeated recycling , Because logic is complex, it needs all kinds of cycles and judgments , It's easy to delete one more time , You just deleted , Others applied for that space , Then you repeatedly delete , In this way, the data of others is deleted by mistake .2. Forget to recycle , This is called a memory leak .
So in order to solve this problem , It's launched java and go Two languages .java Mainly engaged in business development ,go Mainly doing middleware , Development in operation and maintenance . therefore jvm Mainly solved java Two questions of , One is cross platform , One is garbage collection .
Who will do the work ?jvm There is a special buddy named GC, that GC How to find garbage ? The technical term is Root Searching —— Root reachability algorithm . Take a simple example :main The way is the entrance ,main The reference defined in the method is the root , Start from the root object , See whether the root object points to other objects , If you search from the root , An object cannot be found , The line is broken. , That means the object is not referenced , Then this object is garbage , Even if two objects refer to each other , As long as the thread is broken from the root , I can't find you two from the root , Then these two objects are a pair of garbage .
GC roots What are they? ? What are the roots ?mian After method startup ,new The object of (jvm Stack ), Called native Method ( Native Method Stack ), Constant pool created in run constant, Static object of method area , And loaded clazz object .
Find the garbage , How to clean it up ? Mainly used 3 Species algorithm , No matter what kind of garbage collector it is , Can't get out of here 3 Species algorithm , It's nothing more than this 3 Flexible combination of algorithms in .
1. Mark clear : Mark it first , And then just kill . This is the simplest , But it will lead to fragmentation of memory space . Because the space blocks applied for at run time are continuous , When it's recycled , The location of the garbage block is random , Directly mark after cleaning , It becomes a flower block . This leads to : Obviously, the total space is enough , But there is a big object that can't be put behind , The debris space in front cannot be used , This leads to a waste of space . Here's the picture :
2. Copy or copy : Is to use half of the memory each time , Then copy the useful data to the other half of the space , Then the original half is left with garbage , Clean up , Very fast , Because a continuous space is full of garbage . This is efficient and simple , But space is wasted , Typically space for time .
3. Tag compression : I not only found garbage , And clean it up , Then move all the useful objects to the front . The advantage is to make up for the shortcomings of mark removal , Space is free , New objects can be placed behind . The disadvantage is low efficiency .
Memory tuning management is specific to the garbage collector , So the garbage collector is the first . There are 10 A garbage collector , In fact, it is flexible to use the above 3 Species algorithm . Garbage recyclers are used together , The following connection means that it can be matched in pairs .
The commonly used generational model has three kinds of coordination , The three kinds of coordination are used in pairs .
ParNew + CMS,Serial + Serial Old,PS + PO. These three kinds of coordination are generational models . at present jdk1.8 The default is PS + PO This combination . What is a generational model , As shown in the figure below :
It is to divide the space in the heap memory according to the new generation and the old generation , This division is a logical division , It's not physically separated . The new generation and the old generation are according to 1 : 2 The proportional distribution of , The Cenozoic era is divided into eden Area and two survivor District ( Or call it from and to), The ratio is 8 : 1 : 1. Why should it be divided like this ? This is a division to improve recycling efficiency .
New objects first enter eden District ,eden The area will produce a large number of objects , So it takes up a large proportion , But most of the objects inside even 90% All the above objects will become garbage , that eden There is a lot of garbage in the district , What algorithm should be used for recycling ? Since there are many, we should consider the speed , So copy algorithm is selected , The cleaning speed is fast . If you use mark removal or mark compression , that 90% Garbage , You have to mark first , And then remove , So it's not worth it .
First of all , New objects enter eden District , The first garbage collection , Then how to recycle ? take eden The zone is divided into two , Only half ? No, it isn't , because eden There are fewer objects in the area 10%, The remaining objects probably account for 5%—10%,survivor The district has enough space to undertake . So it will be less than 10% Copy useful objects of to survivor District (from),eden Garbage is left in the whole area , It'll be done soon ! Then there will be new objects , Enter the second recycling , At this time, recycling is eden Area and from District , take eden Area and from The useful objects of the area are copied to another survivor District (to), And then eden Area and from The area is cleared ! Then run , To the third recycling , here from The area has been emptied , Just recycle eden Area and to District , take eden Area and to The useful objects of the area are copied to from District , take eden Area and to Clean up the area . And so on , Two survivor Change back and forth in the area . But the object is in two survior When the district comes to change , Without changing the age, add 1, When you get old 15 Then I entered the old generation , What algorithm was used in the old days , There is a lot of space in the old age , It's a new generation 2 times , It may be that it took a long time for the old age to be fully occupied before recycling , At this time, use mark compression or mark removal . How old is the age from the young generation to the old age ?CMS The default is 6, Everything else is default 15, This is related to choosing which garbage collector . This is in jdk Of java Set in the object layout , This age threshold can be set , adopt -XX:MaxTenuringThreshold To set up , Why is the biggest 15 Well , Because it's for age This parameter is assigned 4 individual bit position , The biggest is 15.
Notice how many GC The concept of is easy to confuse :
This is to set the space size ,-Xms -Xmx Is to set the size of the entire heap memory ,-Xmn Is to set the space size of the younger generation in the heap .FullGC Is to recycle garbage as a whole ,YGC It's the garbage collection of the young generation , Just know what it means , No concept .
Let's say so much today , Subsequent updates , You are welcome to correct !
边栏推荐
- VIM command mode and input mode switching
- NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
- @What happens if bean and @component are used on the same class?
- What is high cohesion and low coupling?
- Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
- [neural network] convolutional neural network CNN [including Matlab source code 1932]
- Up meta - Web3.0 world innovative meta universe financial agreement
- Steps of redis installation and self startup configuration under CentOS system
- Upgrade from a tool to a solution, and the new site with praise points to new value
- Have you ever met flick Oracle CDC, read a table without update operation, and read it repeatedly every ten seconds
猜你喜欢
问题:先后键入字符串和字符,结果发生冲突
Completion report of communication software development and Application
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
总结了200道经典的机器学习面试题(附参考答案)
消息队列消息丢失和消息重复发送的处理策略
@What happens if bean and @component are used on the same class?
The road to success in R & D efficiency of 1000 person Internet companies
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
MATLAB實現Huffman編碼譯碼含GUI界面
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
随机推荐
30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
idea 2021中文乱码
Flet教程之 15 GridView 基础入门(教程含源码)
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
Completion report of communication software development and Application
Some opinions and code implementation of Siou loss: more powerful learning for bounding box regression zhora gevorgyan
TypeScript 接口继承
EPP+DIS学习之路(1)——Hello world!
Flet教程之 18 Divider 分隔符组件 基础入门(教程含源码)
Common locking table processing methods in Oracle
【神经网络】卷积神经网络CNN【含Matlab源码 1932期】
Mastering the new functions of swiftui 4 weatherkit and swift charts
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
MATLAB實現Huffman編碼譯碼含GUI界面
数据库系统原理与应用教程(009)—— 概念模型与数据模型
[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)
Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
HCIA复习整理