# jdk18 regionsize变动

2022.06.25 15:37 214浏览

# regionsize计算

``````  size_t region_size = G1HeapRegionSize;
// G1HeapRegionSize = 0 means decide ergonomically.
if (region_size == 0) {
region_size = clamp(max_heap_size / HeapRegionBounds::target_number(),
HeapRegionBounds::min_size(),
HeapRegionBounds::max_ergonomics_size());
}
``````

``````template<typename T>
inline T clamp(T value, T min, T max) {
assert(min <= max, "must be");
return MIN2(MAX2(value, min), max);
}
``````

clamp是一个模板，主要作用是让value的值在min和max之间。

``````  static const size_t MIN_REGION_SIZE = 1024 * 1024;

static const size_t MAX_ERGONOMICS_SIZE = 32 * 1024 * 1024;

static const size_t TARGET_REGION_NUMBER = 2048;
``````

HeapRegionBounds::target_number()就是2048。HeapRegionBounds::min_size()是1m,HeapRegionBounds::max_ergonomics_size()是32m。

``````  // is beneficial in most cases.
region_size = round_up_power_of_2(region_size);
// Now make sure that we don't go over or under our limits.
region_size = clamp(region_size, HeapRegionBounds::min_size(), HeapRegionBounds::max_size());
``````

region_size向上取最接近的2的n次。

``````  static const size_t MAX_REGION_SIZE = 512 * 1024 * 1024;
``````

``````  size_t average_heap_size = (initial_heap_size + max_heap_size) / 2;
region_size = MAX2(average_heap_size / TARGET_REGION_NUMBER, (uintx) MIN_REGION_SIZE);
int region_size_log = log2_long((jlong) region_size)
``````

only considering max heap size when determining region size
rounding up the region size to the nearest power of 2 instead of rounding down

# 小结

1. regionsize如果想超过32m，得主动通过参数G1HeapRegionSize设置。
2. 默认情况下，regionsize的计算只和max_heap_size（xmx）相关。

0人点赞

JAVA开发工程师

1.6万

377

### 风间影月说签约讲师

50篇手记，涉及Java、MySQL、Redis、Spring等方向

### Tony Bai 说签约讲师

142篇手记，涉及Go、C、Java、Python等方向

• 推荐
• 评论
• 收藏
• 共同学习，写下你的评论

0/150