BAT p3 straight series: four kinds of JVM garbage collection algorithm, garbage collection and summary

Garbage collection algorithm

1. Mark Clear

Mark - sweep garbage collection algorithm is divided into two phases: phase marking and clearance phase.

In the first stage marked by the root node (GC Roots), mark all objects starting from the root, not marked unreferenced object is garbage objects. Then, during the cleanup phase, remove all objects unmarked.

BAT p3 straight series: four kinds of JVM garbage collection algorithm, garbage collection and summary

Applications:

  •  Case more live objects more efficient
  •  Suitable for older generations (ie old generation)

Disadvantages:

  •  Prone to memory fragmentation, when again a relatively large objects (typically: the size of the object is greater than each of the free list size but less together and wherein two children), would trigger a garbage collection in advance
  •  Scanning the entire space twice (the first time: mark live objects; second: Clear the object is not marked)

2. Copy the algorithm

A set of nodes from the root scan and mark all of the live objects, and copy these new live objects together into memory (the lower side in FIG memory that together) up, after that the original memory together (FIG. on top of that together memory) out of full recovery

BAT p3 straight series: four kinds of JVM garbage collection algorithm, garbage collection and summary

Now commercial virtual machines are using this collection algorithm to recover the new generation.

Applications:

  •  Fewer cases of live objects more efficient
  •  Scanning the entire space once (mark live objects move and copy)
  •  Suitable for younger generations (ie, the new generation): 98% of the target will essentially be "raw evening toward the dead," the few surviving

Disadvantages:

  •  Together we need to empty memory space
  •  You need to copy moving objects

3. Mark Finishing

Copy the efficiency of the algorithm is built on the survival of small objects, objects and more garbage premise.

This often happens in the new generation, but in the old years the more common situation is that most objects are live objects. If you still use the copy algorithm, due to more live objects, copying costs will also be high.

BAT p3 straight series: four kinds of JVM garbage collection algorithm, garbage collection and summary

Mark - compression algorithm is an algorithm recycling of old age, it is mark - sweep done some basic optimization algorithms.

First time also need to mark starting from the root to make up for all objects, but after that, it does not simply clean up the object is not marked, but all live objects compressed into one end of the memory. After that, clean up all the space outside the boundary. This method not only avoids the generation of debris, and do not need two of the same memory space, and therefore, its cost is relatively high.

4. generational collection algorithm

分代收集算法就是目前虚拟机使用的回收算法,它解决了标记整理不适用于老年代的问题,将内存分为各个年代。一般情况下将堆区划分为老年代(Tenured Generation)和新生代(Young Generation),在堆区之外还有一个代就是永久代(Permanet Generation)。

在不同年代使用不同的算法,从而使用最合适的算法,新生代存活率低,可以使用复制算法。而老年代对象存活率搞,没有额外空间对它进行分配担保,所以只能使用标记清除或者标记整理算法。

BAT p3 straight series: four kinds of JVM garbage collection algorithm, garbage collection and summary

垃圾回收机制

年轻代分为Eden区和survivor区(两块儿:from和to),且Eden:from:to==8:1:1。

BAT p3 straight series: four kinds of JVM garbage collection algorithm, garbage collection and summary

 

jvm内存结构

1)新产生的对象优先分配在Eden区(除非配置了-XX:PretenureSizeThreshold,大于该值的对象会直接进入年老代);

2)当Eden区满了或放不下了,这时候其中存活的对象会复制到from区。

这里,需要注意的是,如果存活下来的对象from区都放不下,则这些存活下来的对象全部进入年老代。之后Eden区的内存全部回收掉。

3)之后产生的对象继续分配在Eden区,当Eden区又满了或放不下了,这时候将会把Eden区和from区存活下来的对象复制到to区(同理,如果存活下来的对象to区都放不下,则这些存活下来的对象全部进入年老代),之后回收掉Eden区和from区的所有内存。

4)如上这样,会有很多对象会被复制很多次(每复制一次,对象的年龄就+1),默认情况下,当对象被复制了15次(这个次数可以通过:-XX:MaxTenuringThreshold来配置),就会进入年老代了。

5)当年老代满了或者存放不下将要进入年老代的存活对象的时候,就会发生一次Full GC(这个是我们最需要减少的,因为耗时很严重)。

垃圾回收有两种类型:Minor GC 和 Full GC。

1.Minor GC

对新生代进行回收,不会影响到年老代。因为新生代的 Java 对象大多死亡频繁,所以 Minor GC 非常频繁,一般在这里使用速度快、效率高的算法,使垃圾回收能尽快完成。

2.Full GC

也叫
Major GC,对整个堆进行回收,包括新生代和老年代。由于Full GC需要对整个堆进行回收,所以比Minor
GC要慢,因此应该尽可能减少Full GC的次数,导致Full
GC的原因包括:老年代被写满、永久代(Perm)被写满和System.gc()被显式调用等。

垃圾回收算法总结

1.年轻代:复制算法

1) 所有新生成的对象首先都是放在年轻代的。年轻代的目标就是尽可能快速的收集掉那些生命周期短的对象。

2)
新生代内存按照8:1:1的比例分为一个eden区和两个survivor(survivor0,survivor1)区。一个Eden区,两个
Survivor区(一般而言)。大部分对象在Eden区中生成。回收时先将eden区存活对象复制到一个survivor0区,然后清空eden区,当这个survivor0区也存放满了时,则将eden区和survivor0区存活对象复制到另一个survivor1区,然后清空eden和这个survivor0区,此时survivor0区是空的,然后将survivor0区和survivor1区交换,即保持survivor1区为空,
如此往复。

3) 当survivor1区不足以存放 eden和survivor0的存活对象时,就将存活对象直接存放到老年代。若是老年代也满了就会触发一次Full GC(Major GC),也就是新生代、老年代都进行回收。

4) 新生代发生的GC也叫做Minor GC,MinorGC发生频率比较高(不一定等Eden区满了才触发)。

2.年老代:标记-清除或标记-整理

1) experienced the object N times the garbage collector is still alive in the young generation, the older generation will be put. Therefore, it is considered the old generation are stored in some of the longer life cycle of the object.

2) memory is also much larger than the new generation (roughly the ratio is 1: 2), when the trigger Major GC old's memory is full That is Full GC, Full GC occurrence frequency is relatively low, the old target's survival time is relatively long, high survival mark .

This more than the young generation and the old generation using different collection algorithm is called a way of "generational collection algorithm", which is used in the current business

3. Each algorithm will have a lot of different garbage collector to realize, in actual use, enough to make a choice according to their operational characteristics.

Guess you like

Origin www.cnblogs.com/mxb0611/p/12081734.html