At present, the garbage collection of commercial virtual machines uses the "generational collection" algorithm, which divides memory into "new generation" and "old age" according to the survival period of the object. Then according to the different years
The characteristics of the generation take different collection algorithms. In the Cenozoic, the replication algorithm is used because every minor GC discovers that many objects die and survive very little. And in the old years,
Like the survival rate is very high, and there is no additional memory space to allocate the security of the use of the "tag-clean" or "mark-tidy" algorithm for recycling. But basically the "mark-and-Finish" calculation
method, the "Mark-clean" algorithm uses very little.
The following is the "copy algorithm" and "tag-collation" algorithm to the text of the way to introduce to you:
First, the replication algorithm (young generation)
1). Principle:
It divides the available memory by capacity into two blocks of equal size, using only one piece at a time. When a piece is exhausted, it copies the surviving object to the other
And then clean up the used memory space once.
2). Benefits:
Each time a memory is collected for one piece, memory allocations do not have to consider complex situations such as memory fragmentation. Just move the heap top pointer, allocate memory sequentially, implement simple,
Run efficiently.
3). Disadvantages:
Reduce the memory to half of the original, wasting half of the space.
4). Copy process diagram (from network)
5). Applications in the JVM
This recycling method used by the younger generation of the JVM, by default in the JVM, divides the young generation of heap space into Eden:Survivor:Survivor according to the 8:1:1 ratio. Each time a virtual machine uses only
Eden and one of the survivor areas. The other survivor space is empty. When recycled, the objects that are still alive in Eden and Surivor are copied once to another.
Surivor space, and then cleans up the Eden and the previously used surivor space. So each new generation can use 90% of the space.
It may be doubtful if the objects that are still alive occupy more than 10% of the space. When the survivor space is not enough, can rely on the old age to allocate the guarantee.
Objects that cannot be accommodated in survivor go straight into the old age. But the premise is that there is room for these objects in the old age. If the warranty fails, full GC will be performed.
Second, marking-finishing algorithm (old age)
1). Principle:
The algorithm is divided into "tagging", "finishing" two steps, first mark out all the objects that need to be recycled, then move all the surviving objects to one end, and then directly clean out the memory outside the end of the boundary.
2). Advantages:
Memory reclamation does not result in memory fragmentation.
3). Disadvantages:
Objects need to be moved, the cost relative to the "mark-clean" algorithm is higher.
4). Copy process diagram (from network)
5). Applications in the JVM:
In the old age, because of the high survival rate of the object, there is no additional space to guarantee it, you must use the "mark-sweep" or "mark-sweep" algorithm for recycling. But basically
Use the "mark-and-organize" algorithm.
Indicate:
1). " Tag-Clean "and" mark-organize "are very similar, except that when the mark is complete, it cleans up the recyclable objects directly. This way leads to a lot of fragmentation and it is also the most basic
The collection algorithm.
2). The diagrams used in the article refer to the network (http://blog.csdn.net/java2000_wl/article/details/8022293).
---------------------------------------------------------------------------Copyright Notice------------------------------------------ ------------------------------------------------
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Blog Address: http://blog.csdn.net/mr_smile2014
Analysis of Java Virtual Machine (JVM) garbage collection algorithm