IT大道IT大道

首页 >  技术 > 垃圾收集的一些概念

垃圾收集的一些概念

原文 http://blog.csdn.net/seraphous/article/details/53688991 2016-12-16 10:12:04 0 评论
  • 垃圾收集算法
    回收机制:以“GC Roots”为根,做可达性分析。可作为GC Roots的对象包括:1)虚拟机栈中引用的对象;2)类静态属性引用的对象;3)方法区中常量引用的对象;4)本地方法栈中JNI(即native方法)引用的对象。

    因为各个年代的对象都有自己的特点,所以分别采用不同的回收算法。     对新生代,回收时对象死亡比较多,采用复制算法回收。将内存分为一块较大的Eden和两块较小的Survivor空间,将Eden和一块Survivor中还活着的对象往另一块Survivor中复制。No fragmentation(碎片) actually takes place inside Eden or Survivor spaces. The write pointer is always residing on the top of the used pool. 对于大多数应用程序, stop-the-world暂停时间可以忽略的小(This is true if most of the objects in Eden can be considered garbage and are never copied to Survivor/Old spaces)。
    对老年代,采用标记-清除或标记-整理算法。

  • 垃圾收集器概述
关于垃圾收集器语境中的并行与并发:     (区分操作系统中提到的并行与并发)     并行(Parellel):指多条垃圾收集线程并行工作,但此时用户线程仍然处于等待状态。
    并发(Concurrent):指用户线程与垃圾收集器线程同时执行(但不一定是并行的,可能会交替执行),用户线程在继续运行,而垃圾收集器运行于另一个CPU上。
  • 垃圾收集器种类
    回收young generation和tenured generation的垃圾收集器不同,且选定一者的collector后也会限制另一者的选择。

串行收集器(serial collector):     包含serial和serial old收集器。     The serial collector uses a single thread to perform all garbage collection work, which makes it relatively efficient because there is no communication overhead between threads. It is best-suited to single processor machines, because it cannot take advantage of multiprocessor hardware, although it can be useful on multiprocessors for applications with small data sets (up to approximately 100 MB).

并行收集器(parallel collector):     包含ParNew、parallel scavenge和parellel old收集器     ParNew就是serial的多线程版本。parallel scavenge则是可以控制吞吐量(运行用户代码时间/CPU消耗总时间)的收集器,适合在后台运算而不需要太多交互的任务。
并发收集器(mostly concurrent collector):     包含GMS和G1收集器。追求响应时间。     并发的消耗:The mostly concurrent collector trades processor resources (which would otherwise be available to the application) for shorter major collection pause times.
    CMS(Concurrent Mark Sweep)收集器:是以获取最短回收停顿时间为目标的收集器。适合于B/S的服务器。总体上说,内存回收过程与用户线程一起执行。用于回收老年代。CMS is designed for applications that prefer shorter garbage collection pauses and that can afford to share processor resources with the garbage collector while the application is running. Typically applications that have a relatively large set of long-lived data (a large tenured generation) and run on machines with two or more processors tend to benefit from the use of this collector. However, this collector should be considered for any application with a low pause time requirement.
    G1收集器:The Garbage-First (G1) garbage collector is a server-style garbage collector, targeted for multiprocessor machines with large memories. It attempts to meet garbage collection (GC) pause time goals with high probability while achieving high throughput. Whole-heap operations, such as global marking, are performed concurrently with the application threads. This prevents interruptions proportional to heap or live-data size.     G1收集器通过以下技术达到高性能和暂停时间目标:     The heap is partitioned into a set of equally sized heap regions, each a contiguous range of virtual memory. G1 performs a concurrent global marking phase to determine the liveness of objects throughout the heap. After the marking phase completes, G1 knows which regions are mostly empty. It collects these regions first, which often yields a large amount of free space. This is why this method of garbage collection is called Garbage-First. As the name suggests, G1 concentrates its collection and compaction activity on the areas of the heap that are likely to be full of reclaimable objects, that is, garbage. G1 uses a pause prediction model to meet a user-defined pause time target and selects the number of regions to collect based on the specified pause time target.     G1 copies objects from one or more regions of the heap to a single region on the heap, and in the process both compacts and frees up memory. This evacuation is performed in parallel on multiprocessors to decrease pause times and increase throughput. Thus, with each garbage collection, G1 continuously works to reduce fragmentation. This is beyond the capability of both of the previous methods. CMS (Concurrent Mark Sweep) garbage collection does not do compaction. Parallel compaction performs only whole-heap compaction, which results in considerable pause times.     需要注意G1并不是实时收集器:It meets the set pause time target with high probability but not absolute certainty. Based on data from previous collections, G1 estimates how many regions can be collected within the target time. Thus, the collector has a reasonably accurate model of the cost of collecting the regions, and it uses this model to determine which and how many regions to collect while staying within the pause time target.     G1的首要关注点是给需要大容量堆和有限GC延迟的应用程序提供解决方案,即6GB及以上大小的堆以及小于0.5秒的稳定及可预测的暂停时间。     如果使用CMS或者并行压缩(the with parallel compaction)的应用程序具有如下一个或多个特征,切换到G1后将会获得收益:     1) More than 50% of the Java heap is occupied with live data.     2) The rate of object allocation rate or promotion varies significantly.     3) The application is experiencing undesired long garbage collection or compaction pauses (longer than 0.5 to 1 second).

    G1长期的目标是取代CMS。Comparing G1 with CMS reveals differences that make G1 a better solution. One difference is that G1 is a compacting collector. Also, G1 offers more predictable garbage collection pauses than the CMS collector, and allows users to specify desired pause targets.

    As with CMS, G1 is designed for applications that require shorter GC pauses.

    Figure 9-1 Heap Division by G1


Description of "Figure 9-1 Heap Division by G1"

    G1在逻辑上来说也是分代的:A set of empty regions is designated as the logical young generation. In the figure, the young generation is light blue. Allocations are done out of that logical young generation, and when the young generation is full, that set of regions is garbage collected (a young collection). In some cases, regions outside the set of young regions (old regions in dark blue) can be garbage collected at the same time. This is referred to as a mixed collection. In the figure, the regions being collected are marked by red boxes. The figure illustrates a mixed collection because both young regions and old regions are being collected. The garbage collection is a compacting collection that copies live objects to selected, initially empty regions. Based on the age of a surviving object, the object can be copied to a survivor region (marked by "S") or to an old region (not specifically shown). The regions marked by "H" contain humongous objects that are larger than half a region and are treated specially; see the section Humongous Objects and Humongous Allocations in Garbage-First Garbage Collector.


  • Minor GC, Major GC, Full GC
Minor GC
    新生代GC:Eden区没有足够的空间分配对象时,虚拟机发起一次MinorGC
Major GC
    老年代GC:升到老年代的对象大于老年代剩余空间时
Full GC
    清理整个堆空间—包括年轻代和老年代。     但是区分Full GC和Major GC是很困难的。To start with – many Major GCs are triggered by Minor GCs, so separating the two is impossible in many cases(因此,两者连起来成了Full GC). On the other hand – many modern garbage collections perform cleaning the Tenured space partially, so again, using the term “cleaning” is only partially correct. JVM将Major GC报告为Full GC。     所以不要去关心到底是Major还是Full:you should focus to finding out whether the GC at hand stopped all the application threads or was it able to progress concurrently with the application threads.
  • 参考
官方指南:

https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/

转载请注明出处!


Tags:JVM

标签列表