Study notes four: garbage collection algorithm

First, mark - sweep algorithm (Mark - Sweep)

       Mark - sweep algorithm is divided into "mark" and "clear" in two stages:

       First mark the objects need to be recovered, the recovery of all unified object is marked after marking the completion of its tagging process is to determine the object is not garbage objects, you can refer to the previous article - to determine whether an object has survived https: // www. cnblogs.com/lveyHang/p/11834028.html

       This recovery algorithm drawbacks:

       1.1 Efficiency

       Mark and sweep efficiency of the two processes are not high.

       1.2 space issues

       Will produce a large number of discrete memory fragmentation mark after clearing space debris could cause too much time in the future need to allocate large objects in the program is running, you can not find enough contiguous memory and had to trigger another garbage collection operation in advance.

image

Second, the replication algorithm (Copying)

       2.1 Background

       In order to solve the mark - sweep efficiency of the algorithm, the emergence of replication algorithm.

       2.2 Algorithm

       The available memory is divided into two equal size, uses only one of them. When this piece of memory is used up, it will also copy the live objects to another piece of memory, and the memory space has been used once cleared away. Such that for every half-area of ​​the entire memory recall, also do not consider the complexities of memory fragmentation isochronous memory allocation, as long as the top of the stack pointer movement, in order to allocate memory.

       2.3 advantages and disadvantages

       Simple, efficient operation. However, the cost of this algorithm is to reduce the memory to half of the original.

       Use on 2.4 virtual machine

       Now commercial virtual machines are using this collection algorithm to recover the new generation, IBM's research shows that 98% of the new generation of the object is "raw evening toward death", and therefore does not require a 1: 1 ratio to divide the memory space , but the memory is divided into a large space and two smaller Eden Survivor spaces, and wherein each use a Survivor Eden. When recycling, Eden and Survivor will survive once the object is copied to another piece on Survivor space, Eden finally clean out Survivor and just used space. HotSpot VM Eden and Survivor default size ratio is 8: 1, i.e. each new generation the available memory space for the new generation of 90% of the whole capacity (80% + 10%), only 10% of the memory is "wasted . "

       When Survivor space is not enough space to store objects on the survival of a young generation collections down, these objects will go directly to the old year by allocating guarantee mechanism (Handle Promotion).

image

Third, the mark - Collation Algorithm (Mark - Compact)

       3.1 Background

       Copying collection algorithm will be carried out more replication objects at higher survival rate, efficiency will be low. If you do not want to waste 50% of the space, we need to have additional space allocation guarantees in response to the memory used by all objects are 100% survival in extreme cases, years old and therefore can not be directly copied selection algorithm.

       3.2 Algorithm

       "Mark - finishing" process of the algorithm is still marked with the "mark - sweep" algorithm, like, and let all live objects are moved to the end, and then clean out the direct memory other than the end border on it.

image

Guess you like

Origin www.cnblogs.com/lveyHang/p/11853501.html