java garbage collection algorithms introduced

Garbage collection is a very important part of the java, most people regard this technology as a product of half java language. In fact, GC history older than java. This article describes the garbage collection algorithm in java, and due to the large number of garbage collection algorithm design program details, and virtual memory operating method for each platform and not the same, so only briefly.

Mark - sweep algorithm

He java is the most basic collection algorithm, as its name suggests, Sun Ah divided into "mark" and "clear" in two stages, first mark all need to be recovered objects, uniform recycling all objects are marked after marking the completion of . The reason that he is the most basic collection algorithm because subsequent collection algorithms which are based on ideas and make improvements to its insufficient.

Mark - sweep algorithm it has two shortcomings: 1, efficiency, marking and removal efficiency of both processes is not high; 2, space issues, will generate a lot of discrete memory fragmentation mark after clearing space debris may be too much when the future will result in the need to allocate large objects in the program is running, you can not find enough contiguous memory and had to depart early for another garbage collection action.

Mark - sweep algorithm execution as shown below:

 

Replication algorithm

The main efficient replication algorithm to solve the problem, he will be in accordance with the available memory capacity is divided into two equal size, each with only one of them. When this piece of memory runs out, the copy will also survive object to another one on, then put the memory has been used once, oh that cheap clean out. So that every time an entire half stack memory recovery zone, also when the memory allocation is not considered complexities of memory fragmentation etc., as long as the top of the stack pointer movement, in order to allocate memory, simple, operation University. But the cost of this algorithm is to reduce by half the memory, the price a little high. Replication algorithm implementation process is as follows:

 

Current commercial virtual machines are using this algorithm to recover the new generation, the company specializes in IBM showed that 98% of the new generation of the object toward evening students dead, it does not require a 1: 1 ratio of columns to divide the memory space, instead the memory space into a larger and two smaller Eden Survivor spaces, and wherein each use a Survivor Eden. When recycling, the Eden and Survivor also alive objects one-off piece of Survivor copied to another space, and finally clean out Survivor Eden and just used space.

The proportion of Eden and Survivor default virtual machine is 8: 1, that is, every new generation of memory available in 90% of the total new generation capacity, only 10% will be wasted. Of course, only 98% of the recoverable data objects in a scene in general, we can not guarantee that every recovery are only more than 10% of live objects, when Survivor space is not enough, need to rely on other memory (in this case is years old) allocation guarantee.

Mark - Collation Algorithm

Copy higher collection algorithm is necessary to carry out more operations in the object replication viability, efficiency will be low. More to the point you do not want to waste space, you need to have additional space allocation guarantees to deal with extreme cases of memory used by all objects are alive, it is generally not use this algorithm directly in the old era.

According to a feature of old age, he proposed another "mark - finishing" algorithm, the tagging process is still in the "mark - sweep" algorithm is the same, but not a direct follow-up steps to clean up the heap of recyclable objects, but to all living objects to moving one end, and then clean out the direct memory outside end border, such as clean-up steps:

 

Attach survival algorithm java object  java object survival algorithm

description link: https: //www.jianshu.com/p/b992a08bbd45 if infringement please contact the author deleted
 

Published 10 original articles · won praise 0 · Views 4425

Guess you like

Origin blog.csdn.net/xiaohuihui501/article/details/103934270