內(nèi)容簡(jiǎn)介
本文主要針對(duì)于綜合層面上進(jìn)行分析JVM優(yōu)化方案總結(jié)和列舉調(diào)優(yōu)參數(shù)計(jì)劃。主要包含:
- 調(diào)優(yōu)之逃逸分析(棧上分配)
- 調(diào)優(yōu)之線程局部緩存(TLAB)
- 調(diào)優(yōu)之G1回收器
棧上分配與逃逸分析
-XX:+DoEscapeAnalysis
逃逸分析(Escape Analysis)
逃逸分析的基本行為就是分析對(duì)象動(dòng)態(tài)作用域:當(dāng)一個(gè)對(duì)象在方法中被定義后,它可能被外部方法所引用,稱為方法逃逸。
方法逃逸的幾種方式如下:
public class EscapeTest {
public static Object obj;
// 給全局變量賦值,發(fā)生逃逸
public void globalVariableEscape() {
obj = new Object();
}
// 方法返回值,發(fā)生逃逸
public Object methodEscape() {
return new Object();
}
// 實(shí)例引用發(fā)生逃逸
public void instanceEscape() {
test(this);
}
}
棧上分配
棧上分配是Java虛擬機(jī)提供的一種優(yōu)化技術(shù)
基本思想
“對(duì)于那些線程私有的對(duì)象(指的是不可能被其他線程訪問的對(duì)象),可以將它們直接分配在棧上,而不是分配在堆上”。
分配在棧上的好處:可以在函數(shù)調(diào)用結(jié)束后自行銷毀,而不需要垃圾回收器的介入,減輕GC壓力,從而提升系統(tǒng)的性能。
使用場(chǎng)景
線程私有對(duì)象
-
受虛擬機(jī)??臻g的約束,適用小對(duì)象,大對(duì)象無法觸發(fā)虛擬機(jī)棧上分配。
-
線程私有變量,大對(duì)象虛擬機(jī)會(huì)分配到TLAB中,TLAB(Thread Local Allocation Buffer)
-
在棧上分配該對(duì)象的內(nèi)存,當(dāng)棧幀從Java虛擬機(jī)棧中彈出,就自動(dòng)銷毀這個(gè)對(duì)象。減小垃圾回收器壓力。
虛擬機(jī)內(nèi)存邏輯圖
JVM內(nèi)存分配源碼:
new關(guān)鍵字直接進(jìn)行分配內(nèi)存機(jī)制,源碼如下:
CASE(_new): {
u2 index = Bytes::get_Java_u2(pc+1);
ConstantPool* constants = istate->method()->constants();
// 如果目標(biāo)Java類已經(jīng)解析
if (!constants->tag_at(index).is_unresolved_klass()) {
// Make sure klass is initialized and doesn't have a finalizer
Klass* entry = constants->slot_at(index).get_klass();
assert(entry->is_klass(), "Should be resolved klass");
Klass* k_entry = (Klass*) entry;
assert(k_entry->oop_is_instance(), "Should be InstanceKlass");
InstanceKlass* ik = (InstanceKlass*) k_entry;
// 如果符合快速分配場(chǎng)景
if ( ik->is_initialized() && ik->can_be_fastpath_allocated() ) {
size_t obj_size = ik->size_helper();
oop result = NULL;
// If the TLAB isn't pre-zeroed then we'll have to do it
bool need_zero = !ZeroTLAB;
if (UseTLAB) {
result = (oop) THREAD->tlab().allocate(obj_size);
}
// 如果TLAB分配失敗,就在Eden區(qū)分配
if (result == NULL) {
need_zero = true;
// Try allocate in shared eden
retry:
// 指針碰撞分配
HeapWord* compare_to = *Universe::heap()->top_addr();
HeapWord* new_top = compare_to + obj_size;
if (new_top <= *Universe::heap()->end_addr()) {
if (Atomic::cmpxchg_ptr(new_top, Universe::heap()->top_addr(), compare_to) != compare_to) {
goto retry;
}
result = (oop) compare_to;
}
}
if (result != NULL) {
// Initialize object (if nonzero size and need) and then the header
// TLAB區(qū)清零
if (need_zero ) {
HeapWord* to_zero = (HeapWord*) result + sizeof(oopDesc) / oopSize;
obj_size -= sizeof(oopDesc) / oopSize;
if (obj_size > 0 ) {
memset(to_zero, 0, obj_size * HeapWordSize);
}
}
if (UseBiasedLocking) {
result->set_mark(ik->prototype_header());
} else {
result->set_mark(markOopDesc::prototype());
}
result->set_klass_gap(0);
result->set_klass(k_entry);
// 將對(duì)象地址壓入操作數(shù)棧棧頂
SET_STACK_OBJECT(result, 0);
// 更新程序計(jì)數(shù)器PC,取下一條字節(jié)碼指令,繼續(xù)處理
UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1);
}
}
}
// Slow case allocation
// 慢分配
CALL_VM(InterpreterRuntime::_new(THREAD, METHOD->constants(), index),
handle_exception);
SET_STACK_OBJECT(THREAD->vm_result(), 0);
THREAD->set_vm_result(NULL);
UPDATE_PC_AND_TOS_AND_CONTINUE(3, 1);
}
代碼總體邏輯
JVM再分配內(nèi)存時(shí),總是優(yōu)先使用快分配策略,當(dāng)快分配失敗時(shí),才會(huì)啟用慢分配策略。
- 如果Java類沒有被解析過,直接進(jìn)入慢分配邏輯。
- 快速分配策略,如果沒有開啟棧上分配或者不符合條件則會(huì)進(jìn)行TLAB分配。
- 快速分配策略,如果TLAB分配失敗,則嘗試Eden區(qū)分配。
- 如果Eden區(qū)分配失敗,則進(jìn)入慢分配策略。
- 如果對(duì)象滿足直接進(jìn)入老年代的條件,那就直接進(jìn)入老年代分配。
- 快速分配,對(duì)于熱點(diǎn)代碼,如果開啟逃逸分析,JVM自會(huì)執(zhí)行棧上分配或者標(biāo)量替換等優(yōu)化方案。
在某些場(chǎng)景使用棧上分配
設(shè)置JVM運(yùn)行參數(shù):
-Xmx10m -Xms10m -XX:+DoEscapeAnalysis -XX:-UseTLAB -XX:+PrintGC
開啟逃逸模式,關(guān)閉TLAB
/**
* @description 開啟逃逸模式,關(guān)閉線程本地緩存模式(TLAB)(jdk1.8默認(rèn)開啟)
* -Xmx10m -Xms10m -XX:+DoEscapeAnalysis -XX:-UseTLAB -XX:+PrintGC
*/
public class AllocationOnStack {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
for (int index = 0; index < 100000000; index++) {
allocate();
}
long end = System.currentTimeMillis();
System.out.println((end - start)+" ms");
Thread.sleep(1000*1000);
// 看后臺(tái)堆情況,來佐證關(guān)閉逃逸優(yōu)化后,是走的堆分配。
}
public static void allocate() {
byte[] bytes = new byte[2];
bytes[0] = 1;
bytes[1] = 1;
}
}
運(yùn)行結(jié)果
[GC (Allocation Failure) 2048K->520K(9728K), 0.0008938 secs]
[GC (Allocation Failure) 2568K->520K(9728K), 0.0006386 secs]
6 ms
jstat -gc pid
查看內(nèi)存使用情況:
結(jié)論
看出棧上分配機(jī)制的速度非???,只需要6ms就完成了實(shí)現(xiàn)GC
調(diào)整JVM運(yùn)行參數(shù)
關(guān)閉逃逸模式,開啟TLAB
-Xmx10m -Xms10m -XX:-DoEscapeAnalysis -XX:+UseTLAB -XX:+PrintGC
查看內(nèi)存使用情況:
運(yùn)行結(jié)果
[GC (Allocation Failure) 2048K->504K(9728K), 0.0013831 secs]
[GC (Allocation Failure) 2552K->512K(9728K), 0.0010576 secs]
[GC (Allocation Failure) 2560K->400K(9728K), 0.0022408 secs]
[GC (Allocation Failure) 2448K->448K(9728K), 0.0006095 secs]
[GC (Allocation Failure) 2496K->416K(9728K), 0.0010540 secs]
[GC (Allocation Failure) 2464K->464K(8704K), 0.0007620 secs]
[GC (Allocation Failure) 1488K->381K(9216K), 0.0007714 secs]
[GC (Allocation Failure) 1405K->381K(9216K), 0.0004409 secs]
[GC (Allocation Failure) 1405K->381K(9216K), 0.0004725 secs]
.......
[GC (Allocation Failure) 2429K->381K(9728K), 0.0008293 secs]
[GC (Allocation Failure) 2429K->381K(9728K), 0.0009006 secs]
[GC (Allocation Failure) 2429K->381K(9728K), 0.0005553 secs]
[GC (Allocation Failure) 2429K->381K(9728K), 0.0005077 secs]
894 ms
結(jié)論
可以看出來,關(guān)閉了棧上分配后,不但YGC次數(shù)增加了,并且總體事件也變長(zhǎng)了,總體事件894ms
調(diào)整JVM運(yùn)行參數(shù)
-Xmx10m -Xms10m -XX:-DoEscapeAnalysis -XX:-UseTLAB -XX:+PrintGC
關(guān)閉逃逸,關(guān)閉TLAB
運(yùn)行結(jié)果
[GC (Allocation Failure) 2048K->472K(9728K), 0.0007073 secs]
[GC (Allocation Failure) 2520K->528K(9728K), 0.0009216 secs]
[GC (Allocation Failure) 2576K->504K(9728K), 0.0005897 secs]
[GC (Allocation Failure) 2551K->424K(9728K), 0.0005780 secs]
[GC (Allocation Failure) 2472K->440K(9728K), 0.0006923 secs]
[GC (Allocation Failure) 2488K->456K(8704K), 0.0006277 secs]
[GC (Allocation Failure) 1480K->389K(9216K), 0.0005560 secs]
.......
[GC (Allocation Failure) 2437K->389K(9728K), 0.0003227 secs]
[GC (Allocation Failure) 2437K->389K(9728K), 0.0004264 secs]
[GC (Allocation Failure) 2437K->389K(9728K), 0.0004396 secs]
[GC (Allocation Failure) 2437K->389K(9728K), 0.0002773 secs]
[GC (Allocation Failure) 2437K->389K(9728K), 0.0002766 secs]
1718 ms
查看內(nèi)存使用情況:
運(yùn)行結(jié)果對(duì)比
- 運(yùn)行耗時(shí)(開啟逃逸 VS關(guān)閉逃逸(開啟TLAB)VS關(guān)閉逃逸(關(guān)閉TLAB)):
6ms VS 894ms VS 1718ms - 虛擬機(jī)內(nèi)存&回收(開啟逃逸VS關(guān)閉逃逸):
調(diào)整分配空間大小
/**
* @since 2019/8/13 上午6:55
* -Xmx10m -Xms10m -XX:-DoEscapeAnalysis -XX:+UseTLAB -XX:+PrintCommandLineFlags -XX:+PrintGC
*/
public class AllocationOnStack {
private static final int _1B = 65;
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
for (int index = 0; index < 100000000; index++) {
allocateBigSpace();
}
long end = System.currentTimeMillis();
System.out.println(end - start);
Thread.sleep(1000*1000);
// 看后臺(tái)堆情況,來佐證關(guān)閉逃逸優(yōu)化后,是走的堆分配。
}
public static void allocate() {
byte[] bytes = new byte[2];
bytes[0] = 1;
bytes[1] = 1;
}
public static void allocateBigSpace() {
byte[] allocation1;
allocation1 = new byte[1 * _1B];
}
}
運(yùn)行結(jié)果
-XX:+DoEscapeAnalysis -XX:InitialHeapSize=5242880 -XX:MaxHeapSize=5242880 -XX:+PrintCommandLineFlags -XX:+PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:-UseTLAB
[GC (Allocation Failure) 1023K->516K(5632K), 0.0028410 secs]
[GC (Allocation Failure) 1540K->578K(5632K), 0.0023265 secs]
........
[GC (Allocation Failure) 2466K->1442K(5632K), 0.0013395 secs]
[GC (Allocation Failure) 2466K->1442K(5632K), 0.0004367 secs]
8925
調(diào)整啟動(dòng)參數(shù): -XX:+DoEscapeAnalysis -XX:-UseTLAB
運(yùn)行結(jié)果:
-XX:+DoEscapeAnalysis -XX:InitialHeapSize=5242880 -XX:MaxHeapSize=5242880 -XX:+PrintCommandLineFlags -XX:+PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:-UseTLAB
[GC (Allocation Failure) 1023K->516K(5632K), 0.0028410 secs]
[GC (Allocation Failure) 1540K->578K(5632K), 0.0023265 secs]
........
[GC (Allocation Failure) 2466K->1442K(5632K), 0.0013395 secs]
[GC (Allocation Failure) 2466K->1442K(5632K), 0.0004367 secs]
8925
經(jīng)過對(duì)比得出結(jié)論:
分配內(nèi)存為>64byte == -XX:-UseTLAB
經(jīng)過多次測(cè)試發(fā)現(xiàn)當(dāng)_1B=64b時(shí)效率還是非常高,一旦大于64b就會(huì)急劇下降。所以推斷出64byte是JVM選擇是TLAB分配 OR Eden區(qū)分配的臨界值。
TLAB的基本介紹
TLAB(Thread Local Allocation Buffer)
線程本地分配緩存,這是一個(gè)線程獨(dú)享的內(nèi)存分配區(qū)域。
特點(diǎn)
-
TLAB解決了:直接在線程共享堆上安全分配帶來的線程同步性能消耗問題(解決了指針碰撞)。
-
TLAB內(nèi)存空間位于Eden區(qū)。
-
默認(rèn)TLAB大小為占用Eden Space的1%。
開啟TLAB的參數(shù)
- -XX:+UseTLAB
- -XX:+TLABSize
- -XX:TLABRefillWasteFraction
- -XX:TLABWasteTargetPercent
- -XX:+PrintTLAB
TLAB的源碼
TLAB的數(shù)據(jù)結(jié)構(gòu)
class ThreadLocalAllocBuffer: public CHeapObj<mtThread> {
HeapWord* _start; // address of TLAB
HeapWord* _top; // address after last allocation
HeapWord* _pf_top; // allocation prefetch watermark
HeapWord* _end; // allocation end (excluding alignment_reserve)
size_t _desired_size; // desired size (including alignment_reserve)
size_t _refill_waste_limit; // hold onto tlab if free() is larger than this
}
- _start 指TLAB連續(xù)內(nèi)存起始地址。
- _top 指TLAB當(dāng)前分配到的地址。
- _end 指TLAB連續(xù)內(nèi)存截止地址。
- _desired_size 是指TLAB的內(nèi)存大小。
- _refill_waste_limit 是指最大的浪費(fèi)空間。默認(rèn)值為64b
eg:假設(shè)為_refill_waste_limit=5KB:
-
假如當(dāng)前TLAB已經(jīng)分配96KB,還剩下4KB可分配,但是現(xiàn)在new了一個(gè)對(duì)象需要6KB的空間,顯然TLAB的內(nèi)存不夠了,4kb<5kb這時(shí)只浪費(fèi)4KB的空間,在_refill_waste_limit之內(nèi),這時(shí)可以申請(qǐng)一個(gè)新的TLAB空間,原先的TLAB交給Eden管理。
-
假如當(dāng)前TLAB已經(jīng)分配90KB,還剩下10KB,現(xiàn)在new了一個(gè)對(duì)象需要11KB,顯然TLAB的內(nèi)存不夠了,這時(shí)就不能簡(jiǎn)單的拋棄當(dāng)前TLAB,這11KB會(huì)被安排到Eden區(qū)進(jìn)行申請(qǐng)。
分配規(guī)則
-
obj_size + tlab_top <= tlab_end,直接在TLAB空間分配對(duì)象。
-
obj_size + tlab_top >= tlab_end && tlab_free > tlab_refill_waste_limit,
-
對(duì)象不在TLAB分配,在Eden區(qū)分配。(tlab_free:剩余的內(nèi)存空間,tlab_refill_waste_limit:允許浪費(fèi)的內(nèi)存空間)
-
tlab剩余可用空間>tlab可浪費(fèi)空間,當(dāng)前線程不能丟棄當(dāng)前TLAB,本次申請(qǐng)交由Eden區(qū)分配空間。
-
-
obj_size + tlab_top >= tlab_end && tlab_free < _refill_waste_limit,重新分配一塊TLAB空間,在新的TLAB中分配對(duì)象。
- tlab剩余可用空間<tlab可浪費(fèi)空間,在當(dāng)前允許可浪費(fèi)空間內(nèi),重新申請(qǐng)一個(gè)新TLAB空間,原TLAB交給Eden。
- 清單:/src/share/vm/memory/ThreadLocalAllocationBuffer.inline.hpp
- 功能:TLAB內(nèi)存分配
inline HeapWord* ThreadLocalAllocBuffer::allocate(size_t size) {
invariants();
// 獲取當(dāng)前top
HeapWord* obj = top();
if (pointer_delta(end(), obj) >= size) {
// successful thread-local allocation
#ifdef ASSERT
// Skip mangling the space corresponding to the object header to
// ensure that the returned space is not considered parsable by
// any concurrent GC thread.
size_t hdr_size = oopDesc::header_size();
Copy::fill_to_words(obj + hdr_size, size - hdr_size, badHeapWordVal);
#endif // ASSERT
// This addition is safe because we know that top is
// at least size below end, so the add can't wrap.
// 重置top
set_top(obj + size);
invariants();
return obj;
}
return NULL;
}
實(shí)際上虛擬機(jī)內(nèi)部會(huì)維護(hù)一個(gè)叫作refill_waste的值,當(dāng)剩余對(duì)象空間大于refill_waste時(shí),會(huì)選擇在堆中分配,若小于該值,則會(huì)廢棄當(dāng)前TLAB,新建TLAB來分配對(duì)象。
這個(gè)閾值可以使用TLABRefillWasteFraction來調(diào)整,它表示TLAB中允許產(chǎn)生這種浪費(fèi)的比例。
默認(rèn)值為64,即表示使用約為1/64的TLAB空間作為refill_waste。
-
TLAB和refill_waste都會(huì)在運(yùn)行時(shí)不斷調(diào)整的,使系統(tǒng)的運(yùn)行狀態(tài)達(dá)到最優(yōu)。
-
如果想要禁用自動(dòng)調(diào)整TLAB的大小,可以使用-XX:-ResizeTLAB禁用ResizeTLAB
-
使用-XX:TLABSize手工指定一個(gè)TLAB的大小。
指針碰撞&Eden區(qū)分配
// 指針碰撞分配
HeapWord* compare_to = *Universe::heap()->top_addr();
HeapWord* new_top = compare_to + obj_size;
if (new_top <= *Universe::heap()->end_addr()) {
if (Atomic::cmpxchg_ptr(new_top, Universe::heap()->top_addr(), compare_to) != compare_to) {
goto retry;
}
result = (oop) compare_to;
}
}
Eden區(qū)指針碰撞,需要模擬多線程并發(fā)申請(qǐng)內(nèi)存空間。
/**
* @since 2019/8/19 下午11:25
-Xmx100m -Xms100m -XX:-DoEscapeAnalysis -XX:+UseTLAB
-XX:TLABWasteTargetPercent=1 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails
*/
public class AllocationTLABSomeThread {
private static final int threadNum = 100;
private static CountDownLatch latch = new CountDownLatch(threadNum);
private static final int n = 50000000 / threadNum;
private static void alloc() {
byte[] b = new byte[100];
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < threadNum; i++) {
new Thread(() -> {
for (int j = 0; j < n; j++) {
alloc();
}
latch.countDown();
}).start();
}
try {
latch.await();
} catch (InterruptedException e) {
System.out.println("hello world");
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
且需要關(guān)閉逃逸分析 -XX:-DoEscapeAnalysis -XX:+UseTLAB
運(yùn)行結(jié)果
-XX:-DoEscapeAnalysis -XX:InitialHeapSize=104857600 -XX:MaxHeapSize=104857600 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:TLABWasteTargetPercent=1 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:+UseTLAB
[GC (Allocation Failure) [PSYoungGen: 25600K->960K(29696K)] 25600K->968K(98304K), 0.0019559 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 26560K->960K(29696K)] 26568K->968K(98304K), 0.0022243 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 26560K->768K(29696K)] 26568K->776K(98304K), 0.0022446 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
........
[GC (Allocation Failure) [PSYoungGen: 32768K->0K(33280K)] 34193K->1425K(101888K), 0.0014598 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 32768K->0K(33280K)] 34193K->1425K(101888K), 0.0015168 secs] [Times: user=0.00 sys=0.01, real=0.00 secs]
823
Heap
PSYoungGen total 33280K, used 3655K [0x00000007bdf00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 32768K, 11% used [0x00000007bdf00000,0x00000007be291c48,0x00000007bff00000)
from space 512K, 0% used [0x00000007bff80000,0x00000007bff80000,0x00000007c0000000)
to space 512K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000)
ParOldGen total 68608K, used 1425K [0x00000007b9c00000, 0x00000007bdf00000, 0x00000007bdf00000)
object space 68608K, 2% used [0x00000007b9c00000,0x00000007b9d64798,0x00000007bdf00000)
Metaspace used 4255K, capacity 4718K, committed 4992K, reserved 1056768K
class space used 477K, capacity 533K, committed 640K, reserved 1048576K
關(guān)閉逃逸和TLAB分配 -XX:-DoEscapeAnalysis -XX:-UseTLAB 運(yùn)行結(jié)果:
-XX:-DoEscapeAnalysis -XX:InitialHeapSize=104857600 -XX:MaxHeapSize=104857600 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:TLABWasteTargetPercent=1 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:-UseTLAB
[GC (Allocation Failure) [PSYoungGen: 25599K->976K(29696K)] 25599K->984K(98304K), 0.0023516 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 26575K->880K(29696K)] 26583K->888K(98304K), 0.0015459 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 26480K->832K(29696K)] 26488K->840K(98304K), 0.0006776 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
.......
[GC (Allocation Failure) [PSYoungGen: 32767K->0K(33280K)] 34053K->1285K(101888K), 0.0004838 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 32767K->0K(33280K)] 34053K->1285K(101888K), 0.0005389 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
5388
Heap
PSYoungGen total 33280K, used 21392K [0x00000007bdf00000, 0x00000007c0000000, 0x00000007c0000000)
eden space 32768K, 65% used [0x00000007bdf00000,0x00000007bf3e4230,0x00000007bff00000)
from space 512K, 0% used [0x00000007bff00000,0x00000007bff00000,0x00000007bff80000)
to space 512K, 0% used [0x00000007bff80000,0x00000007bff80000,0x00000007c0000000)
ParOldGen total 68608K, used 1285K [0x00000007b9c00000, 0x00000007bdf00000, 0x00000007bdf00000)
object space 68608K, 1% used [0x00000007b9c00000,0x00000007b9d41788,0x00000007bdf00000)
Metaspace used 4248K, capacity 4718K, committed 4992K, reserved 1056768K
class space used 478K, capacity 533K, committed 640K, reserved 1048576K
經(jīng)過對(duì)比,相差7倍左右。二者內(nèi)存回收??,從YoungGC次數(shù)和耗時(shí)上沒有太大變化:應(yīng)為都是Eden區(qū)分配。
G1垃圾回收過程
觸發(fā)混合回收條件:
-XX:InitiatingHeapOccupancyPercent=45 ,當(dāng)老年代空間使用占整個(gè)堆空間45%時(shí)。
混合回收范圍:
新生代、老年代、大對(duì)象。
混合回收過程:
初始標(biāo)記:
-
這個(gè)過程會(huì)STW,停止系統(tǒng)線程。
-
標(biāo)記GC-Roots的直接引用對(duì)象。
- 線程棧中局部變量表 。
- 方法區(qū)中的靜態(tài)變量/常量等。
- 本地方法棧。
- 特點(diǎn):速度極快。
并發(fā)標(biāo)記
- 這個(gè)過程不會(huì)STW,系統(tǒng)線程正常運(yùn)行。
- 從第一階段標(biāo)記的GC-Roots開始追蹤所有存活對(duì)象。
- 特點(diǎn):慢,很耗時(shí)。
- 優(yōu)化:JVM會(huì)對(duì)“并發(fā)標(biāo)記”階段新產(chǎn)生的對(duì)象及對(duì)象修改做記錄(RememberSet)
最終標(biāo)記:
- 這個(gè)過程會(huì)STW,系統(tǒng)線程停止運(yùn)行。
- 會(huì)根據(jù)“并發(fā)標(biāo)記”階段記錄的RememberSet進(jìn)行對(duì)象標(biāo)記。
- 特點(diǎn):很快。
- RememberSet相當(dāng)于是拿空間換時(shí)間。
混合回收:
- 這個(gè)過程會(huì)STW,系統(tǒng)線程停止運(yùn)行。
- 會(huì)計(jì)算老年代中每個(gè)Region中存活對(duì)象數(shù)量,存活對(duì)象占比,執(zhí)行垃圾回收預(yù)期耗時(shí)和效率。
-
耗時(shí):會(huì)根據(jù)啟動(dòng)參數(shù)中
-XX:MaxGCPauseMillis=200
和歷史回收耗時(shí)來計(jì)算本次要回收多少老年代Region才能耗時(shí)200ms。 -
特點(diǎn):回收了一部分遠(yuǎn)遠(yuǎn)沒有達(dá)到回收的效果,G1還有一個(gè)特殊處理方法,STW后進(jìn)行回收,然后恢復(fù)系統(tǒng)線程,然后再次STW,執(zhí)行混合回收掉一部分Region,
‐XX:G1MixedGCCountTarget=8
(默認(rèn)是8次),反復(fù)執(zhí)行上述過程8次。
注意:假設(shè)要回收400個(gè)Region,如果受限200ms,每次只能回收50個(gè)Region,反復(fù)8次剛好全部回收完畢。這么做的好處是避免單次停頓回收STW時(shí)間太長(zhǎng)。
-
**還有一個(gè)參數(shù)要提一下
‐XX:G1HeapWastePercent=5 (默認(rèn)是5%)
。- 混合回收是采用復(fù)制算法,把要回收的Region中存活的對(duì)象放入其他Region中。
- 然后這個(gè)Region中的垃圾全部清理掉,這樣就會(huì)不斷有Region釋放出來,當(dāng)釋放出的Region占整個(gè)堆空間5%時(shí),停止混合回收。
-
還有一個(gè)參數(shù):
‐XX:G1MixedGCLiveThresholdPercent=85 (默認(rèn)值85%)
?;厥誖egion的時(shí)候,必須是存活對(duì)象低于85%。
混合回收失敗時(shí):
-
在Mixed回收的時(shí)候,無論是年輕代還是老年代都是基于復(fù)制算法進(jìn)行回收,都要把各個(gè)Region的存活對(duì)象拷貝到另外其他的Region里去,萬一拷貝是發(fā)生空間不足,就會(huì)觸發(fā)一次失敗。文章來源:http://www.zghlxwxcb.cn/news/detail-823904.html
-
一旦回收失敗,立馬就會(huì)切換采用Serial 單線程進(jìn)行標(biāo)記+清理+整理,整個(gè)過程是非常慢的(災(zāi)難)。文章來源地址http://www.zghlxwxcb.cn/news/detail-823904.html
到了這里,關(guān)于【JVM故障問題排查心得】「Java技術(shù)體系方向」Java虛擬機(jī)內(nèi)存優(yōu)化之虛擬機(jī)參數(shù)調(diào)優(yōu)原理介紹的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!