当前位置:网站首页>JVM detailed parsing
JVM detailed parsing
2022-07-23 13:18:00 【JavaSupeMan】
One 、java Code compilation and execution principle
take Java The code is compiled into .class file , Is a binary bytecode file , Then through the interpreter , Interpreted as machine code , Last in CPU Implementation of
JVM Introduction of components
CPU( register ): remember jvm The execution address of the next instruction in .CPU As a program counter .
characteristic :
1. Threads are private
2. There will be no memory overflow
Stack : Thread runtime , A memory space required , It consists of stack frames , A stack frame corresponds to a method call , The stack frame mainly stores method parameters , local variable , Return address and other information . The stack is visible only to threads . So it's thread private , His life cycle is the same as threads .
-Xss Set size
reflection :
1. Does stack memory need garbage collection ?
answer : Unwanted , Because the stack will pop up after each method call is executed , Automatically released
2. Whether the stack space is as large as possible ?
answer : No , The increase of stack memory will reduce the number of execution threads , Because the size of physical memory is fixed . The increase of stack memory is only able to make more recursive calls to methods
3. Whether local variables in methods are thread safe ?
answer :1. If the local variable does not escape the scope of the method , Is thread safe
2. If the local variable references an object , And escape the scope of action , It's not thread safe .
4. Under what circumstances will stack memory overflow be caused ?
answer : Too many stack frames , Cause stack memory overflow , For example, recursive call , End condition is not set
Stack frame too large , Cause memory overflow , Relatively rare
The use of third-party libraries may also lead to , such as fastjson, When two objects are referenced circularly , Call transfer json Method will cause memory overflow
Add : Locate thread methods that occupy too much memory on the virtual machine
Case study 1:CPU Take up too much
1. Use top command , View and find occupancy cpu The highest PID
2. Use command ps Find the specific thread ID
ps H -eo pid,tid,%cpu | grep 32655
3. Use jstack command , Follow the process ID You can list all threads
jstack 32655
Then find the thread ID32665 Convert to hex , yes 7F99, Then look in the print thread 
After finding it, you can see , Which line of code is wrong
Native Method Stack : Not by java How to write code , because java The code has certain limitations , It cannot directly deal with the bottom of the operating system , So I need some C or C++ Method calls some functions of the bottom layer
Pile up : It's thread shared , All objects in the heap need to consider thread safety , adopt new Keyword creates objects that use heap memory , There's a garbage collection mechanism . Heap memory can be subdivided into Edon Area and Survivor District ,Survivor Is divided into from Area and to District , The newly created objects are all in Edon District , Different areas GC The mechanism is also different .
-Xmx Set heap space size
Heap memory overflow :OutOfMemoryError
Heap memory diagnostics
1.jps Tools
You can view what is in the current system java process
2.jmap Tools
Check heap memory usage
3.jconsole Tools
Graphic interface , Multifunctional monitoring tools , It can be monitored continuously 
Method area : It's also thread shared , Primary storage Class loading information , Constant pool , Runtime constant pool . stay JDK 7 before , Traditionally, the method area is called permanent generation ( Habitually ), And by the JDK8, Finally, the concept of permanent generation is completely abandoned , Switch to and JRockit、J9 Same Metaspace implemented in local memory (Metaspace) Instead of . The two biggest differences are : The meta space is not in the memory set by the virtual machine , It's using local memory .
Constant pool : It's just a watch , The virtual machine instruction finds the class name to execute according to this constant table , Method name , Parameter type , Literal quantity and other information
Runtime constant pool : The constant pool is *.class In the document , When the class is loaded , Its constant pool information will be put into the runtime constant pool , And change the symbolic address into the real address
Garbage collection mechanism
1. Determine whether the object is recyclable
1. Reference counting :
When an object is referenced by other objects , Count +1, When no longer referenced , Count -1, When count is 0 when , The object will be recycled . But there's a problem , When two objects hold references to each other , That is, when circular reference , Neither of these objects will be recycled , Even if they no longer use , This leads to the problem of memory leakage
2. Accessibility analysis :
First, we need to determine a series of root objects (GC root object ), Root objects are certain objects that cannot be recycled , Before garbage collection , First, all objects in the heap memory will be scanned , Then see whether each object is directly or indirectly referenced by the root object , If it is , Then these objects cannot be recycled .
Root object : Core system classes such as Object class ,hashMap Class, etc. , Or some native class , Some active thread classes , Or some objects that are locking can be used as root objects
2. What are the types of citations , What does it have to do with recycling
Strong citation : They are the ones we often use , such as new The objects that come out are strongly referenced objects , During garbage collection, the object will be recycled only if it is not directly or indirectly referenced by other objects .
Soft citation : Soft quotes in java There's a special SoftReference type , Soft reference means only when there is not enough memory , The referenced object will be recycled .
Weak reference :weakReference and softReference Is very similar , The difference is weekReference The referenced object is just garbage collection execution , It will be recycled , And whether or not there's not enough memory .
Virtual reference :PhantomReference Is used to track the activity of the garbage collector collection object , stay GC In the process of , If you find any PhantomReference,GC The reference will be placed in ReferenceQueue in , By the programmer himself , When the programmer calls ReferenceQueue.pull() Method , Will quote out ReferenceQueue After removal ,Reference The object becomes Inactive state , Means that the referenced object can be recycled .
3. Garbage collection algorithm
1. Tag clearing algorithm : First, through accessibility analysis , Mark objects that are not referenced by the root object , And then clean up . But it will cause memory fragmentation , Some large objects will need some continuous storage space , This will reduce the utilization of memory
characteristic : Fast , But it will cause memory fragmentation
2. Marking algorithm : The difference from Mark clearing is , The current memory usage will be collated , Make it more compact , So that there will be no such memory fragments after recycling
characteristic : Slow speed , But no memory fragmentation
3. Copy algorithm : There are two memory areas of the same size ,From Area and To District , Copy objects that are still in use to To Area memory , And then empty From Area memory space , And then exchange From Area and To Area content .
characteristic : It needs to occupy double the memory space
Garbage collection of virtual machines
The actual garbage collection will not be a collection algorithm , Instead, use a more appropriate garbage collection algorithm for different memory areas
Those with large heap memory can be divided into a new generation , An old age , The Cenozoic is subdivided into three areas , One Edon District , Two survie District ,From Area and To District . The purpose of this division is to make JVM Can better manage the objects in the heap memory , Including memory allocation and recycling . The newly created objects are all in the new generation , More specifically, in edon District , When edon When the area is full , Will start once Minor GC, First, use reachability analysis to mark the objects in the heap , Then use the replication algorithm , take edon Area and from The surviving objects are copied to To District , And set the age to 1, Every time you experience garbage collection, your age will +1. And then From Area and To Exchange area , When From The age of an object in the area reaches 15 when , Will be promoted to the senior generation .
minor GC Will cause a stop the world, stay GC Other threads will be suspended before completion , Only GC After completion, other threads continue to execute
With Minor GC On going , The number of elderly people will continue to grow , As a result, the space in the old age will not be enough , In the end Major GC(MajorGC Faster than Minor GC It's a lot slower , It is said that 10 About times ).Major GC The algorithm used is : Mark clear ( Recycling ) Algorithm or tag compression algorithm . When the new generation and the old generation are full and can't let go , It will trigger once full GC
relevant VM Parameters
Initial heap size -Xms
The maximum size of the pile -Xmx
The size of the new generation -Xmn
-XX:~ The proportion of surviving areas , Promotion threshold , Promotion details, etc
边栏推荐
猜你喜欢

机器学习:李航-统计学习方法(二)感知机+代码实现(原始+对偶形式)

OpenCV图像处理(上)几何变换+形态学操作

High voltage MOS tube knx42150 1500v/3a is applied to frequency converter power supply inverter, etc

如何防止订单重复支付?

记录一次爬虫题库

Step on the electric render process renderer to solve the problem of require is not defined

Summary of time complexity( Ο Is the asymptotic upper bound, Ω is the asymptotic lower bound, P, NP, NP hard, NPC problem)

行业现状令人失望,工作之后我又回到UC伯克利读博了

UI automation

给1万帧视频做目标分割,显存占用还不到1.4GB | ECCV2022
随机推荐
The context of virtual memory technology (Part 1)
信号完整性(SI)电源完整性(PI)学习笔记(三十二)电源分配网路(四)
互联网时代下,如何精细化用户运营?
信号完整性(SI)电源完整性(PI)学习笔记(三十一)电源分配网路(三)
迷茫、工作没动力? 职业发展没希望? 看这篇文章就够了
信號完整性(SI)電源完整性(PI)學習筆記(三十二)電源分配網路(四)
力扣 729. 我的日程安排表 I
[ACTF2020 新生赛]BackupFile 1
Pod topology constraints
问题解决:Script file ‘Scripts\pip-script.py‘ is not present.
Evaluation of classification model
Li Kou 729. My schedule I
UI automation
MySQL - composite query external connection
SAR成像之点目标仿真(三)—— 仿真结果分析
Outlook tutorial, how to switch calendar views and create meetings in outlook?
How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB
Paging collections using streams
Summary of time complexity( Ο Is the asymptotic upper bound, Ω is the asymptotic lower bound, P, NP, NP hard, NPC problem)
行业现状令人失望,工作之后我又回到UC伯克利读博了