194. The way jvm The main components of ？ And its role ？
Class loader （ClassLoader）
Run time data area （Runtime Data Area）
Execution engine （Execution Engine）
Local library interface （Native Interface）
Function of components ： First through the class loader （ClassLoader） Will be able to Java Code into bytecode , Run time data area （Runtime Data Area） Then load the bytecode into memory , And bytecode files are just JVM A set of instruction set specifications , It can't be executed directly by the underlying operating system , So you need a specific command parser execution engine （Execution Engine）, Translate bytecode into underlying system instructions , Give it to CPU To carry out , In this process, you need to call the local library interface of other languages （Native Interface） To implement the function of the entire program .
195. The way jvm Run time data area ？
Program counter 、 Virtual machine stack 、 Native Method Stack 、 Pile up 、 Method area
Some regions exist with the start of virtual machine process , Some regions are created and destroyed depending on the start and end of the user process .
Queues and stacks are used to pre store data .
Queues allow first in, first out retrieval elements , But there are exceptions ,Deque Interface allows elements to be retrieved from both ends .
Stack and queue are very similar , But it runs to retrieve elements last in, first out .
Before introducing the parental delegation model, let's talk about the classloader . For any class , It needs to be established by the class loader that loads it and the class itself JVM Uniqueness in , Every classloader , All have a separate class namespace . Class loader is to set... According to the specified fully qualified name class File loading to JVM Memory , And then convert it to class object .
Classloader classification ：
Start class loader （Bootstrap ClassLoader）, It's part of the virtual machine itself , Used to load Java_HOME/lib/ In the directory , Or be -Xbootclasspath The class library in the path specified by the parameter and recognized by the virtual machine ;
Other class loaders ：
Extend the classloader （Extension ClassLoader）： Responsible for loading libext Directory or Java. ext. dirs All class libraries in the path specified by the system variable ;
Application class loader （Application ClassLoader）. Responsible for loading user class path （classpath） The specified class library on , We can use this classloader directly . General situation , If we don't have a custom classloader, we use this loader by default .
Parent delegation model ： If a class loader receives a class load request , It will not load the class itself first , Instead, delegate the request to the parent loader to complete , This is true of classloaders at every level , In this way, all the load requests will be sent to the boot class loader at the top level , Only when the parent load cannot complete the load request （ The required class was not found in its search scope ） when , The child loader will try to load the class .
Class loading is divided into the following 5 A step ：
load ： Find the corresponding class File and import ;
Check ： Check loaded class The correctness of the document ;
Get ready ： Allocate memory space for static variables in a class ;
analysis ： The process of virtual machine replacing symbolic reference in constant pool with direct reference . Symbolic reference is understood as a sign , In direct reference, it points directly to the address in memory ;
initialization ： Perform initialization on static variables and static code blocks .
There are generally two ways to judge ：
Reference counter ： Create a reference count for each object , Counter with object reference +1, Count when reference is released -1, When the counter is 0 Can be recycled . It has a disadvantage that it can't solve the problem of circular reference ;
Accessibility analysis ： from GC Roots Start looking down , The path a search takes is called a reference chain . When an object arrives GC Roots When no chain of references is connected , This object can be recycled .
Strong citation 、 Soft citation 、 Weak reference 、 Virtual reference （ Ghost quotes / Phantom reference ）
Mark - Clear algorithm
Mark - Sorting algorithm
Serial： The first single threaded serial garbage collector .
Serial Old：Serial Older versions of the garbage collector , It's also single threaded , It can be used as CMS Alternative plan of garbage collector .
ParNew： yes Serial Multithreaded version of .
Parallel and ParNew Collectors are similar to multithreading , but Parallel Is a throughput first collector , We can sacrifice the waiting time for the throughput of the system .
Parallel Old yes Parallel Old generation version ,Parallel Using the memory recovery algorithm of replication ,Parallel Old Using tags - A memory recovery algorithm for defragmentation .
CMS： A kind of collector aiming at obtaining the shortest pause time , just the thing B/S System .
G1： One that takes into account throughput and pause time GC Realization , yes JDK 9 Future defaults GC Options .
CMS It's English Concurrent Mark-Sweep For short , It's a garbage collector that gets the shortest recovery pause time at the expense of throughput . For applications that require server response speed , This kind of garbage collector is very suitable . Start up JVM Plus “-XX:+UseConcMarkSweepGC” To specify the use of CMS Garbage collector .
CMS Using tags - Implemented by clearing algorithm , So in gc A large number of memory fragments are generated , When the remaining memory can not meet the program running requirements , The system will appear Concurrent Mode Failure, temporary CMS Will be used Serial Old Garbage removal by recycler , The performance will be reduced at this time .
205. What are the new generation garbage collectors and old generation garbage collectors ？ What's the difference? ？
New generation recycler ：Serial、ParNew、Parallel Scavenge
Old generation recycler ：Serial Old、Parallel Old、CMS
Whole stack recycler ：G1
The new generation of garbage collectors generally use the replication algorithm , The advantage of replication algorithm is high efficiency , The disadvantage is low memory utilization ; The old generation recycler generally uses the mark - Garbage collection by sorting algorithm .
The generational recycler has two partitions ： The old generation and the new generation , The default space of the new generation accounts for 1/3, The default proportion of the old generation is 2/3.
The new generation uses replication algorithms , In the new generation 3 Zones ：Eden、To Survivor、From Survivor, Their default share is 8:1:1, Its execution process is as follows ：
hold Eden + From Survivor Live objects put To Survivor District ;
Empty Eden and From Survivor Partition ;
From Survivor and To Survivor Zoning exchange ,From Survivor change To Survivor,To Survivor change From Survivor.
Each time the From Survivor To To Survivor Objects that survive on move , age +1, When age arrives 15（ The default configuration is 15） when , Upgrade to old generation . Big target will also directly enter the old generation .
When space occupation reaches a certain value, global garbage collection will be triggered , Generally, we use the execution algorithm of tag collation . All the above steps constitute the overall implementation process of the whole generation of garbage collection .
JDK It comes with a lot of monitoring tools , All in JDK Of bin Under the table of contents , The most common one is jconsole and jvisualvm These two view monitoring tools .
jconsole： Used to deal with JVM Memory in 、 Thread and class monitoring ;
jvisualvm：JDK All around analysis tool , Can be analyzed ： memory dump 、 Thread snapshot 、 Program deadlock 、 Monitor changes in memory 、gc Change, etc .
-Xms2g： The initial push size is 2g;
-Xmx2g： Maximum heap memory is 2g;
-XX:NewRatio=4： Set the memory ratio of the young to the old to 1:4;
-XX:SurvivorRatio=8： Setting up the new generation Eden and Survivor The ratio is 8:2;
–XX:+UseParNewGC： Specify the use of ParNew + Serial Old Garbage collector combination ;
-XX:+UseParallelOldGC： Specify the use of ParNew + ParNew Old Garbage collector combination ;
-XX:+UseConcMarkSweepGC： Specify the use of CMS + Serial Old Garbage collector combination ;
-XX:+PrintGC： Turn on Printing gc Information ;
-XX:+PrintGCDetails： Print gc Details .
This article altogether 2018 Number of words , Average reading time ≈ 6 minute
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .