当前位置:网站首页>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
 Insert picture description here
2. Use command ps Find the specific thread ID
ps H -eo pid,tid,%cpu | grep 32655
 Insert picture description here
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
 Insert picture description here
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
 Insert picture description here

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


原网站

版权声明
本文为[JavaSupeMan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230556124706.html