求助>使用Garbage First (G1),使用jstat -gc 或者jmap 查看内存的使用,显示的是已经使用regions 的个数 * 每个regions大小?>
1回复

使用Garbage First (G1),使用jstat -gc 或者jmap 查看内存的使用,显示的是已经使用regions 的个数 * 每个regions大小?



image.png

image.png

153 阅读
请先登录,再评论

回复列表

墨书3天前

是的,可以参考下下面的代码

void G1MonitoringSupport::recalculate_sizes() {
  G1CollectedHeap* g1 = g1h();

  // Recalculate all the sizes from scratch. We assume that this is
  // called at a point where no concurrent updates to the various
  // values we read here are possible (i.e., at a STW phase at the end
  // of a GC).

  uint young_list_length = g1->young_list()->length();
  uint survivor_list_length = g1->g1_policy()->recorded_survivor_regions();
  assert(young_list_length >= survivor_list_length, "invariant");
  uint eden_list_length = young_list_length - survivor_list_length;
  // Max length includes any potential extensions to the young gen
  // we'll do when the GC locker is active.
  uint young_list_max_length = g1->g1_policy()->young_list_max_length();
  assert(young_list_max_length >= survivor_list_length, "invariant");
  uint eden_list_max_length = young_list_max_length - survivor_list_length;

  _overall_used = g1->used_unlocked();
  _eden_used = (size_t) eden_list_length * HeapRegion::GrainBytes;
  _survivor_used = (size_t) survivor_list_length * HeapRegion::GrainBytes;
  _young_region_num = young_list_length;
  _old_used = subtract_up_to_zero(_overall_used, _eden_used + _survivor_used);

  // First calculate the committed sizes that can be calculated independently.
  _survivor_committed = _survivor_used;
  _old_committed = HeapRegion::align_up_to_region_byte_size(_old_used);

  // Next, start with the overall committed size.
  _overall_committed = g1->capacity();
  size_t committed = _overall_committed;

  // Remove the committed size we have calculated so far (for the
  // survivor and old space).
  assert(committed >= (_survivor_committed + _old_committed), "sanity");
  committed -= _survivor_committed + _old_committed;

  // Next, calculate and remove the committed size for the eden.
  _eden_committed = (size_t) eden_list_max_length * HeapRegion::GrainBytes;
  // Somewhat defensive: be robust in case there are inaccuracies in
  // the calculations
  _eden_committed = MIN2(_eden_committed, committed);
  committed -= _eden_committed;

  // Finally, give the rest to the old space...
  _old_committed += committed;
  // ..and calculate the young gen committed.
  _young_gen_committed = _eden_committed + _survivor_committed;

  assert(_overall_committed ==
         (_eden_committed + _survivor_committed + _old_committed),
         "the committed sizes should add up");
  // Somewhat defensive: cap the eden used size to make sure it
  // never exceeds the committed size.
  _eden_used = MIN2(_eden_used, _eden_committed);
  // _survivor_committed and _old_committed are calculated in terms of
  // the corresponding _*_used value, so the next two conditions
  // should hold.
  assert(_survivor_used <= _survivor_committed, "post-condition");
  assert(_old_used <= _old_committed, "post-condition");
}

其中HeapRegion::GrainBytes就是每个region的大小