One 、 generational
The left figure is the logic diagram of the data area at runtime , On the right is the generational logic diagram , The top part of the picture on the right is JDK1.8 Previous , The picture below is JDK1.8 After that ; The same color in the two pictures represents the same area .
From the picture we can see that , The new generation and the old generation belong to the heap ; stay JDK1.8 Before , The method area is also known as the older generation ,JDK1.8 after , Cancelled the old days , Instead, meta space Meta Space, That is to say 1.8 after , The method area can be realized by changing the old generation into a meta space .
The purpose of designing meta space , One is to avoid the problem of permanent generation overflow ; The second is similar ArrarList, It can automatically expand capacity .
1、 Why generations
Because the life cycle of an object is different , Some objects die day by day , once minorGC When , Probably 98% Objects will be recycled . But some objects have a long life cycle , If heap memory is not generational , All objects have the same area , So it's very complicated to recycle . After generations , Garbage collection can be done in different areas , Use different recycling methods , For example, the new generation GC Recycling is called minor GC, Old age GC Recycling is called GC, also Full GC, At the same time, the new generation and the old generation .
2、 Why is the new generation 8:1:1?
Adopted by the new generation GC The way is to copy the recycle algorithm , I'll talk about it in detail , This is the first time the algorithm has been executed minorGC after , Objects that are still alive will be copied to S0 District , Second execution minorGC,eden and S0 Objects that are still alive in will be copied to S1 District , If the next one comes minorGC after , The living object is S1 There's no room for it , It will be deposited in the old generation .
Our ideal situation is not to enter the old generation . If it is 9:1 Words , for the first time minorGC The surviving objects are copied to S0 District , The next time minorGC, The living object will be put into the old generation , But our ideal situation is not to enter the old generation . So if it is 8:1:1 Words , You can have more cushion , Occupy 8 Share of Eden I can't put it down. , Trigger minor gc, Put the surviving ones in s0;eden Redistribution , And only in Eden To assign ,Eden Trigger again when it can't be put down again minor gc, It's going to recycle Eden and S0 From inside , Put the living in S1; If after some recycling S1 I can't put it down , Put it in the old generation .
All of the above are in vernacular , I hope you can correct me again . Now let's see GC Algorithm .
Let's first look at what memory needs to be recycled ：
Program counter 、 Virtual machine stack 、 The local method stack is the private memory space of each thread , Born with threads , Die with thread . So this 3 A moment's memory allocation and recycling is certain , There's no need to think about memory recycling .
But the method area and the heap are different , Multiple implementation classes of an interface may require different memory , We don't know which objects to create until the program is running , This part of memory allocation and recycling are dynamic ,GC The main focus is on this part of memory . To make a long story short ,GC The main memory for recycling is JVM Method area and heap in .
1、 objects creating
At the linguistic level , Creating an object is usually just a new It's just keywords , In the virtual machine , What is the creation of a common object ？
Virtual opportunity to a new When the command , First, we will check whether the parameters of this instruction can be located in the constant pool to the symbol reference of a class , And check whether the class represented by this symbol reference has been loaded 、 Parsed and initialized . without , You must first perform the class loading process of the response , After the class load check passes , Next, the virtual machine allocates memory for new objects .
2、 Object allocation
There are two ways to assign objects ： Pointer collisions and free lists .
2.1、 Pointer collision , It's suitable for memory regulation , All allocated memory is put aside in a regular way , The other side of leisure , There is a pointer in the middle as an indicator , If the new comer , Just move a part of the free area on the pointer side
2.2、 The space list is suitable for irregular memory , You need a list of spaces to record memory usage
3、 What kind of objects need to be recycled
What kind of objects need to be recycled , That is to say Java Of GC When to recycle garbage . There are two algorithms for judging , To determine when it can be recycled ： Quoting calculation method and reachability analysis
3.1、 Quoting calculation （ abandoned ）
Add a reference counter to the object , Whenever there is a place to quote him , The counter value adds 1; When the reference fails , The counter value is reduced 1; The counter at any time is 0 The object of can be recycled .
But there's a problem with this approach , It's the problem that the loop refers to , If a Refer to the b,b Refer to the a,a Calculated as 1,b The calculation of is also 1, In this way, his two calculations are 1,GC It can't be recycled . At the same time, every time the increase and decrease of counter brings a lot of extra overhead , So in JDK1.1 after , This algorithm is no longer used .
3.2、 Accessibility analysis （ Use ）
It's through a series of things called GC Roots As the starting point , Start with these nodes and drill down , The path a search takes is called a reference chain , When an object goes to any one GC Roots When none of the reference chains are connected , It shows that the object can be recycled .
Which objects can be called GC Roots Well ？
Can it be called GC Roots The key is , If a thread is running a method , So the variable in this method is the survival state , It should not be recycled , So this variable can be used as GC Roots.
So it can be GC Roots There are ：
- Object referenced by local variable table in virtual machine stack
- Object referenced by class static variable and object referenced by constant in method area
- Local method stack JNI Referenced object
4、java What are the types of references in
There are two ways to determine what kind of objects need to be recycled , It's all about quoting ,Java There are four levels of references to objects in , These four levels are from high to low ： Strong citation 、 Soft citation 、 Weak reference 、 Virtual reference . All four references exist in the heap , An object belongs to an inner reference , With his strongest citation decision .
4.1、 Strong citation
We write code every day with strong references 、 If an object is strongly referenced by someone , Then the garbage collector will never recycle it . When memory runs out of space ,Java The virtual machine would rather throw OouOfMemoryError error , Causes the program to terminate abnormally , It will not solve the problem of insufficient memory by randomly recycling objects with strong references .
4.2、 Soft citation SoftReference
If an object has a soft reference , So if there's enough memory , The garbage collector won't recycle it , If there's not enough memory , It will reclaim the memory of these objects . As long as the recycling period doesn't recycle it , This object can be used by the program .
Soft references can be used to implement memory sensitive caching . stay jvm All soft references will be cleared before insufficient memory is reported , thus gc It's possible for mobile phones to be soft accessible , It may solve the problem of tight memory , Avoid memory overflow . When it is collected depends on gc Algorithm and gc Available memory size at run time .
4.3、 Weak reference WeakReference
If an object has only weak references , Then this kind of object is dispensable , Because as long as the object is gc It's going to be wiped out anytime it's scanned . The difference between weak quotation and soft quotation is ： Whether the current memory space is enough or not , as long as gc Scan to recycle at any time .
4.4、 Virtual reference PlantomReference
Virtual quoting is the same as virtual supposition , Different from other quotations , Virtual references do not determine the life cycle of an object . If an object only holds virtual references , Then he's like no quotation , Can be recycled at any time . Virtual references are mainly used to track the garbage collection activities of objects .
5、 Recycling method
There are three ways to recycle ： Mark - Clear algorithm 、 Copy the recycle algorithm 、 Mark - Sorting algorithm 、 There are generational collection algorithms
5.1、 Mark - Clear algorithm
First mark the objects to be recycled , And then we recycle it ; It is suitable for garbage collection with more objects ; The disadvantage is that a large number of discontinuous memory fragments are generated after the tag is cleared , There is not enough contiguous memory space to allocate memory to large objects , Leading to early start of garbage collection .
5.2、 Copy the recycle algorithm
Divide the available memory into two equal sizes , Use only one piece at a time , When this block is used up, the surviving objects are copied to another block , Then clean up the used memory once .
Suitable for garbage collection with fewer living objects ;
The advantage is that every time the whole half area is recycled , Don't worry about memory fragmentation , Just move the top pointer , Allocate memory in order ; Implement a simple , Efficient operation
The disadvantage is that the memory is reduced by half ;
The new generation is using the replication recovery algorithm . Cenozoic memory according to 8:1:1 It is divided into Eden,From Survivor,To Survivor Three spaces , Each use Eden and From Survivor Two spaces to allocate memory to objects , When there is not enough memory for garbage collection , Copy the live object to To Survivor Space , Then clean up Eden and From Survivor Space ; This is equivalent to a waste of memory 10%; If 10% Of To Survivor When the space is not enough to store the living object, the elderly generation is required to allocate guarantee （ Through the distribution guarantee mechanism, the surviving objects will enter the elderly generation directly ）
5.3、 Mark - Sorting algorithm
First mark the objects to be recycled , Move the live object to one end , Finally, clean up the memory outside the end boundary ; In this way, there will be no memory fragmentation after recycling .
5.4、 Generational collection algorithm
Memory is divided into new generation and old generation according to the life cycle of objects , Then according to the characteristics of each era, use the appropriate recycling algorithm ; Such as ： The new generation of living objects can use replication algorithm ; The old generation has a large number of living objects and has no allocation guarantee. It must use the tag cleaning or tag sorting recovery algorithm
6、 Garbage collector
If there is a connection between the two collectors , It means that they can be used together .
6.1、Serial The collector , All other user threads must be suspended during collection , Until the end of the collection .
6.2、parNew The collector , yes Serial Multithreaded version of collector
6.3、Parallel Scavenge The collector , Focus on throughput control , Focus on time utilization
throughput = Run user code time / （ Run user code time + Garbage collection time ） ; If the virtual machine runs in total 100 minute , Collect expenses 1 minute , Then the throughput is 99%
Old age collector
6.4、Serial Old The collector , yes Serial The older version of the single thread collector
6.5、Parallel Old The collector , yes Parallel Scavenge The older version of the multithreaded collector
6.6、CMS The collector （Concurrent Mark Sweep）,CMS It's a concurrent collection , Low pause collector , The focus is on the shortest recovery pause time
6.7、G1 The collector , Using tag - Sorting algorithm , The principle is G1 Divide the new generation and the old generation into independent areas of equal size , Carry out garbage collection in the whole region , The new generation and the old generation are no longer physically isolated , It's all a collection of parts of independent areas ; By calculating the value of garbage accumulation in each area （ Reclaim available space / The time it takes to recycle ）, Then garbage collection is prioritized according to the value , It ensures the efficiency of recycling ;
---------------------------------- I hope you can correct me .-----------------------------------------------
本文为[Wooden pine cat]所创，转载请带上原文链接，感谢