Following the contents of the Java virtual machine on a part of the then write an article about the class is about to load, I write about this one related to garbage collection algorithm, the concept of it,

Previous primer "Java class loader" what you want to see the class loading can take a look.

First, the algorithm is surviving objects

First, we want to reclaim an object, first of all have to know the object is not alive. If the object is not alive to continue to recover, if the object is dead, it should be recovered immediately.

First, we want to determine a class is not alive, there are two general methods more mature.

1, reference notation

  When there is a reference to an object, the object reference number plus 1. When an object reference number 1 is the time, it can be determined that the object is no longer used, or is a dead objects.

However, this algorithm has a problem, problems may arise refer to each other, so that this kind have no way to recover the object, like this below.

In this case, though a and b are not pointing, which also however due to a referenced b, b which references a page. So they have to cause the reference count is always 1. The final is no way to recover the object.

2, root search algorithm

Root search algorithm essentially by setting a series of "GC ROOTS" objects, these objects as a starting point, to start the search down. In the process down search, if the object appeared in the search of the Road King, then that object is live objects. Join now on the search for a path object is no longer any one of the "GC ROOTS" object, then that object is an object dead.

We figure below, take a look.

The figure China to teach deep blue GC Roots is the object, the object of our search through the GC roots down, you can know, Obj 1,2,3 are on the search path GCroots object, but no longer search Obj5,6 Road King on, even if the random number Obj references 5 and 6 are 1, but the two objects is still the object of death, we can remove them later.

Second, the garbage collection algorithm

Now more popular garbage collection algorithm generally has three algorithms, each algorithm each have their own advantages and disadvantages. Here I will brief you.

1. Mark sweep algorithm

Clear labeling The main idea of ​​the algorithm, we will first find those objects that are already dead, then add to the death of these objects on a marker to clean up the area for the late release of memory.

So it can be divided into two stages to deal with, the first stage is the "mark" stage, where the mark is not to say mark just once, but twice to mark.

Two markers:

We root search algorithm, when an object is no longer found on the search path, not to say that this object will be immediately recovered, but there are likely to be recovered. To determine whether an object is really should be recovered need to see the results of the second mark, when finalize method object when subjected to a second marker first determine the object is not called by the JVM has been passed, or there is no If you do not overload overloaded that this object will be immediately marked as recycling must never expect, that is, it passed the second mark, if the object is overloaded, and finalize functions perform more slowly, or where there has been abnormal, or there was an infinite loop may occur so that any object on the search path with reference to this object then this object will be removed immediately complete collection of species already marked, and the garbage collector will not be recycling.

Clear labeling algorithm, those matters through the object after the mark, full recovery out. This algorithm does have a big problem, the first of its marking process and removal process is very time-consuming. And then he will form a lot of memory space debris. As shown below.

I will not draw a direct borrowing someone else draw a good map, we can see the emergence of a number of memory fragments recovered after the death of the object, in fact, this is very detrimental to the program running. If the program is running, we should allocate a relatively large object, it is likely to trigger advance a GC.

 

2. Copy the algorithm (copy compression algorithm)

Replication algorithm is relatively simple, but he also is a prerequisite for the subjects were divided into two complete each GC first live objects moved to one side, then completely removed another side of the object.

We can look at the following diagram:

First, we do the whole memory is divided into two areas, each surviving copy objects to another area. After complete removal of another area. This algorithm completely solves the problem of memory fragmentation, but it created a new problem, that is for memory utilization is not high, the same time there is only a general memory can be utilized.

3. Mark Collation Algorithm

Tags to organize algorithm can solve the problem of the above two algorithms, which addresses the memory fragmentation, but also solve the problem of high memory utilization.

It is an object GC every surviving memory moved to one side and then to the object delimitation survival, clear all objects other than the boundary.

See FIG particular the following effects:

Then we will all live objects to move to the side, clearly all areas directly outside the boundary. This algorithm is the perfect solution to the problem of memory fragmentation, with a more extensive collection algorithm.

Third, the concept of garbage collection

Apart from the above collection algorithm does, there is also a garbage collection concept. Is through a lot of analysis of it, a lot of object creation time soon will die (This refers to those local variables, both towards life objects screen of death), and when part of the object after many times of the GC have not been recovered, basically these objects tend to be more stable, the proportion of deaths would be relatively small.

Our longest use HotSpot virtual machine is using a concept of generational collection. It is divided into memory "permanent generation", "New Generation" and "old year." Kept the different objects in different regions, the new generation of objects can easily be recovered using this concept, greatly reduces recovery and the difficulty of shortening the recovery time. Reducing the mark and sweep time, increase throughput, reduce "STOP THE WORLD" time.

 

 

 

Guess you like

Origin www.cnblogs.com/leiroliu/p/11863704.html