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(); // 最近发生过一次担保失败或者可能发生担保失败
}