Continued previous section, two methods previously enumerated determining whether Java objects recyclable; reachability analysis and reference weight method. Most virtual machine used is reachability analysis method. Here write about garbage collection algorithm Java.
Java garbage collection algorithm has four main algorithm are: mark - sweep method; replication algorithm; mark - finishing law; generational collection algorithm. Details are as follows.
1, mark - sweep method
Marking algorithm is mainly clearance method in two phases; ①, mark all memory segments need to be recovered, ②, recovering the tag memory. There are two methods of such deficiencies. A marking, clear two process performance loss is large, efficiency is too low; Second, the marker will generate a lot of clear memory fragmentation. As shown below:
2, replication algorithm
Copy algorithm application Java program memory is divided into two; used here (A, B representative); using the first memory block A; when the recovery when the GC; occupied all the live objects in memory A fragment memory copy and analyzed to B memory block. A-> B; B-> A in this alternate embodiment. Disadvantages of this method exists; consume a lot of memory, FIG follows:
3, Mark - finishing law
Because some objects higher survival rates are relatively high dependence on other objects (years old can be called); the above two methods can not meet it produces this algorithm: tags to organize law. This method is similar to the cleaning method of marking; different subsequent removal method; flow: Mark -> surviving object moves toward the one end -> Clear Clear side objects. As shown below:
4, generational collection algorithm
Generational collection algorithm main memory into the new generation; old's two kinds. In the new generation of large quantities of the object due to the object it is recyclable; replication algorithm so employed. In the old era due to the large number of objects you do not need to clear the operation; therefore can be used: mark - sweep algorithm or mark - sorting algorithms.