DefNewGeneration

DefNewGeneration内存管理解析
本文介绍了DefNewGeneration类的设计与实现,该类用于管理Java虚拟机中的年轻代内存区域,包括Eden区及两个Survivor区。文章详细阐述了DefNewGeneration的构造函数过程,涉及内存空间的初始化配置、性能计数器的创建、空间边界的计算等,并简述了关键方法如capacity_before_gc、capacity、used等的功能。

defNewGeneration.hpp

class DefNewGeneration: public Generation {
.................
}

defNewGeneration.cpp

构造函数

DefNewGeneration::DefNewGeneration(ReservedSpace rs,
                                   size_t initial_size,
                                   int level,
                                   const char* policy)
  : Generation(rs, initial_size, level),
    _promo_failure_drain_in_progress(false),
    _should_allocate_from_space(false)
{
  MemRegion cmr((HeapWord*)_virtual_space.low(),
                (HeapWord*)_virtual_space.high());
  Universe::heap()->barrier_set()->resize_covered_region(cmr);

  if (GenCollectedHeap::heap()->collector_policy()->has_soft_ended_eden()) { // 默认false
    _eden_space = new ConcEdenSpace(this);
  } else {
    _eden_space = new EdenSpace(this); // Eden
  }
  _from_space = new ContiguousSpace(); // 创建From内存区管理器
  _to_space   = new ContiguousSpace(); // 创建To内存区管理器

  if (_eden_space == NULL || _from_space == NULL || _to_space == NULL)
    vm_exit_during_initialization("Could not allocate a new gen space");

  // Compute the maximum eden and survivor space sizes. These sizes
  // are computed assuming the entire reserved space is committed.
  // These values are exported as performance counters.
  uintx alignment = GenCollectedHeap::heap()->collector_policy()->space_alignment();
  uintx size = _virtual_space.reserved_size();
  _max_survivor_size = compute_survivor_size(size, alignment); // 计算From区和To区的最大大小
  _max_eden_size = size - (2*_max_survivor_size); // //Eden区最大大小

  // allocate the performance counters

  // Generation counters -- generation 0, 3 subspaces  创建相关的计数器
  _gen_counters = new GenerationCounters("new", 0, 3, &_virtual_space);
  _gc_counters = new CollectorCounters(policy, 0);

  _eden_counters = new CSpaceCounters("eden", 0, _max_eden_size, _eden_space,
                                      _gen_counters);
  _from_counters = new CSpaceCounters("s0", 1, _max_survivor_size, _from_space,
                                      _gen_counters);
  _to_counters = new CSpaceCounters("s1", 2, _max_survivor_size, _to_space,
                                    _gen_counters);

  compute_space_boundaries(0, SpaceDecorator::Clear, SpaceDecorator::Mangle);
  update_counters();
  _next_gen = NULL;
  _tenuring_threshold = MaxTenuringThreshold; // 进入老年代的阀值
  _pretenure_size_threshold_words = PretenureSizeThreshold >> LogHeapWordSize;  // 判断是否大对象的值   LogHeapWordSize=3 右移3位

  _gc_timer = new (ResourceObj::C_HEAP, mtGC) STWGCTimer();
}

capacity_before_gc

size_t gen0_capacity = gch->get_gen(0)->capacity_before_gc();

size_t DefNewGeneration::capacity_before_gc() const {
  return eden()->capacity();
}

capacity

可用空间

size_t DefNewGeneration::capacity() const {
  return eden()->capacity()
       + from()->capacity();  // to() is only used during scavenge
}

used

size_t DefNewGeneration::used() const {
  return eden()->used()
       + from()->used();      // to() is only used during scavenge
}

size_t DefNewGeneration::free() const {
  return eden()->free()
       + from()->free();      // to() is only used during scavenge
}

top_addr、end_addr

HeapWord** DefNewGeneration::top_addr() const { return eden()->top_addr(); }
HeapWord** DefNewGeneration::end_addr() const { return eden()->end_addr(); }

ParNewGeneration

// A Generation that does parallel young-gen collection.

class ParNewGeneration: public DefNewGeneration {
  friend class ParNewGenTask;
  friend class ParNewRefProcTask;
  friend class ParNewRefProcTaskExecutor;
  friend class ParScanThreadStateSet;
  friend class ParEvacuateFollowersClosure;

  ...................
}

进入老年代条件

collectorPolicy.cpp:948

bool GenCollectorPolicy::should_try_older_generation_allocation(
        size_t word_size) const {
  GenCollectedHeap* gch = GenCollectedHeap::heap();
  size_t gen0_capacity = gch->get_gen(0)->capacity_before_gc();
  return    (word_size > heap_word_size(gen0_capacity)) // 新生代空间不够  要分配的大小 > 年轻代容量(eden+from总大小) eden()->capacity() + from()->capacity();
         || GC_locker::is_active_and_needs_gc()  // gc locker 被占用(jni 临界区)
         || gch->incremental_collection_failed(); // 最近发生过一次担保失败或者可能发生担保失败
}

DefNewGeneration::swap_spaces

form 和 to 交换

void DefNewGeneration::swap_spaces() {
  ContiguousSpace* s = from();
  _from_space        = to(); // 交换 form 和 to的首地址
  _to_space          = s;
  eden()->set_next_compaction_space(from()); // 设置eden下一片需要压缩的区域为现在的from区
  // The to-space is normally empty before a compaction so need
  // not be considered.  The exception is during promotion
  // failure handling when to-space can contain live objects.
  from()->set_next_compaction_space(NULL); // 新的from区下一片需要压缩的区域为NULL

  if (UsePerfData) {
    CSpaceCounters* c = _from_counters;
    _from_counters = _to_counters;
    _to_counters = c;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值