—————  the second day   —————


Now let's study these three problems together .

problem 1: What needs to be recycled ?

First of all, we need to know how to recycle ? There are two algorithms to determine whether an object needs to be recycled . One is reference counting algorithm , One is reachability analysis algorithm .

Reference counting algorithm

The reference counting algorithm is very simple , It judges the importance of an object by recording the number of times it is referenced . If the object is referenced by another object , Then its reference count is increased by one , If you delete a reference to the object , Then its reference count is reduced by one , When the reference count of the object is 0 Time , Then the object is recycled .

What's wrong with reference counting ? When two objects refer to each other , Since they refer to each other, the count is not zero , This results in two objects that cannot be recycled .

therefore ,Java Virtual machine uses another method to judge whether the object is alive or not , It is the reachability analysis algorithm .

Reachability analysis algorithm

Reachability analysis algorithm , The first step is to identify a series of root objects (GC Roots), From the root object as the starting point, a reference chain is searched according to the reference relationship between objects (Reference Chain)
, Objects in the reference chain survive , Objects that are not in the reference chain are considered recyclable .

There is a very appropriate metaphor : Accessibility analysis algorithm is like cleaning grape strings , We can lift a bunch of grapes from one branch , They're like a chain of references , Objects that are not connected to reference chains are like grapes scattered in a pool , Recyclable .

Objects referenced in the virtual machine stack ( The variable used by the running method , Parameters, etc )

Object referenced by class static property in method area (static Fields declared by keywords )

Object referenced by a constant in the method area ,( that is final Fields declared by keywords )

Objects referenced in the local method stack (native method )

Java Reference within virtual machine .( Of course, things inside the system can be used as roots )

problem 2: What are the important garbage collection algorithms ?

Learn to judge which garbage in memory needs to be recycled , We need to master several important garbage collection algorithms .

sign - Clearing algorithm

sign - Scavenging is the most basic garbage collection algorithm , Generally speaking, it is divided into two steps :


Mark all objects that need to be recycled ( grey ), That is to say, we are judging rubbish .


The part marked gray , Get rid of it .

It should be noted that : The so-called clean-up , There is no need to actually clear the bytes of the whole memory , Just record the start and end address of the free object and put it into the free list , Indicates that this memory is free .

Advantages: fast speed , Just make a mark to know which piece needs to be recycled , But his shortcomings are also fatal .

There are two main shortcomings in him : First, the implementation efficiency is not stable , Second, it will involve the problem of memory fragmentation .

Someone might ask , What does fragmentation mean ? The stack described above , Through the mark clearing method, although the space is cleared , But the memory cleared out is a large number of discontinuous memory fragments , Like the object below , It is clear that there is a place in the whole , But it can't be put in because of discontinuity , This is the mark - Remove the biggest drawback of the algorithm .

The so-called tag replication algorithm and tag collation algorithm , Both are improvements to the shortcomings of the mark removal algorithm , So it is said that the mark clearing algorithm is the most basic way .

sign - Sorting algorithm

And marking - Different clearing algorithms , sign - The sorting algorithm is mobile . It will make all the surviving objects move to one end of the memory space , Then clear the memory beyond the boundary .



What are the disadvantages ? sign - The sorting algorithm involves the movement of objects , In the finishing phase , Due to moving available objects , Need to update references . The efficiency is low .

sign - Replication algorithm

sign - Replication algorithm , It's different from the previous comparison , He divided the memory space into two parts , During garbage collection, live objects in the memory in use are copied to unused memory blocks , Then clear all the objects in the memory block you are using . Finally, exchange the two memory roles , Finally, complete the garbage collection .

Generally speaking, it can be divided into These are the steps :




It's not hard to see , Tag replication algorithm does not need tags, which improves the efficiency . Besides, he doesn't have the problem of parameter fragmentation .

however . The disadvantages of mark copy algorithm are also obvious , It needs double space .

problem 3: What is the specific process of garbage collection ?

Since it is said that JVM Virtual machines don't use a single algorithm , Instead, they combine three algorithms to make them work together , Its concrete realization is java Generational garbage collection mechanism in virtual machine .

As shown in the figure above , namely Java Partition of heap memory . Why do we need to do this ? That's because of us java Object lifetimes are different , Some may take a long time to use , And some may be used up can be thrown away . So we can make a decision according to the principle
Different characteristics of life cycle , Carry out different garbage collection strategies .
in general , The new generation of garbage collection is more frequent , It was a long time ago that garbage collection was triggered . What the new generation deals with are all objects of life and death , And what's recycled in the old days is more valuable , Long lived objects .

Let's take a very understandable example : New generation garbage disposal , It's like dealing with household waste , And the old garbage disposal , It's more like Spring Festival cleaning , There's too much rubbish at home. Let's clean it up again . Clean up the rubbish , They are stored at home for a long time , It may have been useful , Now I'm retired. I'll leave it for the new year and then clean it up .

every time , We create an object , Will occupy a certain memory size in Eden Park , Gradually the garden of Eden was full . When we want to create objects again , You will find that there is not enough space .

At this time , It triggers a garbage collection , The new generation of garbage collection is called MinorGC.

MinorGC After triggering , Eden Park will analyze the accessibility of each object , To know which objects should be cleaned up as garbage .
MinorGC In this case, the label copy algorithm is adopted , It stores useful objects in the survivor area to, And then clear the garden of Eden of objects .

The lucky ones in the survival zone , Will be marked with one “ Lucky value ”, Represents how many times they have resisted cleaning .

last , Survival zone to And survival zone from You need to interact with the location , This is not a physical location exchange , It's about , The two definitions have been exchanged , Next time, the one on the left is the survival zone to, On the right is the survival zone from It's over .

let me put it another way , Survival zone to It's always empty .

Let's simulate it a few more times , Make an impression :

If a lot of people come in again , The garden of Eden is full again !

That would trigger one MinorGC, Move the survivors to the survival zone to, Everything else will be cleared .

Finally, don't forget , Survival zone from And survival zone to Another exchange ” position “.