G1重要参数说明

G1MixedGCCountTarget

G1 mixed GC在计算每次需要回收多少个region时使用,G1 通过并发标记计数出每个region的使用情况,然后通过1-N次mixed gc来进行垃圾回收,那么每次垃圾回收需要回收多少个region呢,其中最小值通过如下逻辑来计算:

uint G1CollectorPolicy::calc_min_old_cset_length() {
  // The min old CSet region bound is based on the maximum desired
  // number of mixed GCs after a cycle. I.e., even if some old regions
  // look expensive, we should add them to the CSet anyway to make
  // sure we go through the available old regions in no more than the
  // maximum desired number of mixed GCs.
  //
  // The calculation is based on the number of marked regions we added
  // to the CSet chooser in the first place, not how many remain, so
  // that the result is the same during all mixed GCs that follow a cycle.

  const size_t region_num = (size_t) _collectionSetChooser->length();
  const size_t gc_num = (size_t) MAX2(G1MixedGCCountTarget, (uintx) 1);
  size_t result = region_num / gc_num;
  // emulate ceiling
  if (result * gc_num < region_num) {
    result += 1;
  }
  return (uint) result;
}

G1OldCSetRegionThresholdPercent

如上所述,计算每次回收的最大region个数逻辑如下,其中使用到了 G1OldCSetRegionThresholdPercent:

uint G1CollectorPolicy::calc_max_old_cset_length() {
  // The max old CSet region bound is based on the threshold expressed
  // as a percentage of the heap size. I.e., it should bound the
  // number of old regions added to the CSet irrespective of how many
  // of them are available.

  G1CollectedHeap* g1h = G1CollectedHeap::heap();
  const size_t region_num = g1h->num_regions();
  const size_t perc = (size_t) G1OldCSetRegionThresholdPercent;
  size_t result = region_num * perc / 100;
  // emulate ceiling
  if (100 * result < region_num * perc) {
    result += 1;
  }
  return (uint) result;
}

G1HeapWastePercent

mixed GC时,如果发现待回收region的可回收内存不大,那么会放弃执行mixed gc,逻辑如下:

 size_t reclaimable_bytes = cset_chooser->remaining_reclaimable_bytes();
  double reclaimable_perc = reclaimable_bytes_perc(reclaimable_bytes);
  double threshold = (double) G1HeapWastePercent;
  if (reclaimable_perc <= threshold) {
    ergo_verbose4(ErgoMixedGCs,
              false_action_str,
              ergo_format_reason("reclaimable percentage not over threshold")
              ergo_format_region("candidate old regions")
              ergo_format_byte_perc("reclaimable")
              ergo_format_perc("threshold"),
              cset_chooser->remaining_regions(),
              reclaimable_bytes,
              reclaimable_perc, threshold);
    return false;
  }

InitiatingHeapOccupancyPercent

老年代使用的内存占整个堆的比例超过InitiatingHeapOccupancyPercent时,才会进行并发标记,默认值为45%;

PrintAdaptiveSizePolicy

打印关于自适应大小调整相关的信息,例如GC时如何选择要回收的region日志

总结

Concurrent Marking Cycle 和Mixed GC是两个不同的阶段:

  1. Concurrent Marking Cycle是发生在yong gc之后,当老年代使用达到一定比例时,yong gc之后会跟随一次Concurrent Marking Cycle; Concurrent Marking Cycle阶段的清理操作并不会清理垃圾对象,也不会执行存活对象的拷贝。也就是说,在极端情况下,该阶段结束之后,空闲分区列表将毫无变化,JVM的内存使用情况也毫无变化;
  2. mixed gc实际上与YGC是一样的:第一个步骤是从分区中选出若干个分区进行回收,这些被选中的分区称为Collect Set(简称CSet);第二个步骤是把这些分区中存活的对象复制到空闲的分区中去,同时把这些已经被回收的分区放到空闲分区列表中。垃圾回收总是要在一次新的YGC开始才会发生;

你可能感兴趣的:(G1重要参数说明)