发布于2021-07-24 21:13 阅读(709) 评论(0) 点赞(1) 收藏(1)
本文主要针对于综合层面上进行分析JVM优化方案总结和列举调优参数计划。主要包含:
-XX:+DoEscapeAnalysis
逃逸分析的基本行为就是分析对象动态作用域:当一个对象在方法中被定义后,它可能被外部方法所引用,称为方法逃逸。
方法逃逸的几种方式如下:
public class EscapeTest {
public static Object obj;
// 给全局变量赋值,发生逃逸
public void globalVariableEscape() {
obj = new Object();
}
// 方法返回值,发生逃逸
public Object methodEscape() {
return new Object();
}
// 实例引用发生逃逸
public void instanceEscape() {
test(this);
}
}
栈上分配是Java虚拟机提供的一种优化技术
“对于那些线程私有的对象(指的是不可能被其他线程访问的对象),可以将它们直接分配在栈上,而不是分配在堆上”。
分配在栈上的好处:可以在函数调用结束后自行销毁,而不需要垃圾回收器的介入,减轻GC压力,从而提升系统的性能。
new关键字直接进行分配内存机制,源码如下:
CASE(_new): {
u2 index = Bytes::get_Java_u2(pc+1);
ConstantPool* constants = istate->method()->constants();
// 如果目标Java类已经解析
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;
// 如果符合快速分配场景
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区分配
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区清零
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);
// 将对象地址压入操作数栈栈顶
SET_STACK_OBJECT(result, 0);
// 更新程序计数器PC,取下一条字节码指令,继续处理
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再分配内存时,总是优先使用快分配策略,当快分配失败时,才会启用慢分配策略。
-Xmx10m -Xms10m -XX:+DoEscapeAnalysis -XX:-UseTLAB -XX:+PrintGC
/**
* @description 开启逃逸模式,关闭线程本地缓存模式(TLAB)(jdk1.8默认开启)
* -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);
// 看后台堆情况,来佐证关闭逃逸优化后,是走的堆分配。
}
public static void allocate() {
byte[] bytes = new byte[2];
bytes[0] = 1;
bytes[1] = 1;
}
}
[GC (Allocation Failure) 2048K->520K(9728K), 0.0008938 secs]
[GC (Allocation Failure) 2568K->520K(9728K), 0.0006386 secs]
6 ms
jstat -gc pid
看出栈上分配机制的速度非常快,只需要6ms就完成了实现GC
-Xmx10m -Xms10m -XX:-DoEscapeAnalysis -XX:+UseTLAB -XX:+PrintGC
[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
可以看出来,关闭了栈上分配后,不但YGC次数增加了,并且总体事件也变长了,总体事件894ms
-Xmx10m -Xms10m -XX:-DoEscapeAnalysis -XX:-UseTLAB -XX:+PrintGC
[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
/**
* @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);
// 看后台堆情况,来佐证关闭逃逸优化后,是走的堆分配。
}
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];
}
}
-XX:+DoEscapeAnalysis -XX:InitialHeapSize=5242880 -XX:MaxHeapSize=5242880 -XX:+PrintCommandLineFlags -XX:+PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:-UseTLAB
1.
[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
调整启动参数: -XX:+DoEscapeAnalysis -XX:-UseTLAB
运行结果:
-XX:+DoEscapeAnalysis -XX:InitialHeapSize=5242880 -XX:MaxHeapSize=5242880 -XX:+PrintCommandLineFlags -XX:+PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC -XX:-UseTLAB
1.
[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
分配内存为>64byte == -XX:-UseTLAB
经过多次测试发现当_1B=64b时效率还是非常高,一旦大于64b就会急剧下降。所以推断出64byte是JVM选择是TLAB分配 OR Eden区分配的临界值。
线程本地分配缓存,这是一个线程独享的内存分配区域。
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
}
eg:假设为_refill_waste_limit=5KB:
inline HeapWord* ThreadLocalAllocBuffer::allocate(size_t size) {
invariants();
// 获取当前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;
}
实际上虚拟机内部会维护一个叫作refill_waste的值,当剩余对象空间大于refill_waste时,会选择在堆中分配,若小于该值,则会废弃当前TLAB,新建TLAB来分配对象。
这个阈值可以使用TLABRefillWasteFraction来调整,它表示TLAB中允许产生这种浪费的比例。
默认值为64,即表示使用约为1/64的TLAB空间作为refill_waste。
// 指针碰撞分配
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区指针碰撞,需要模拟多线程并发申请内存空间。
/**
* @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);
}
}
且需要关闭逃逸分析 -XX:-DoEscapeAnalysis -XX:+UseTLAB
-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
关闭逃逸和TLAB分配 -XX:-DoEscapeAnalysis -XX:-UseTLAB 运行结果:
-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
经过对比,相差7倍左右。二者内存回收♻️,从YoungGC次数和耗时上没有太大变化:应为都是Eden区分配。
-XX:InitiatingHeapOccupancyPercent=45 ,当老年代空间使用占整个堆空间45%时。
新生代、老年代、大对象。
-XX:MaxGCPauseMillis=200
和历史回收耗时来计算本次要回收多少老年代Region才能耗时200ms。‐XX:G1MixedGCCountTarget=8
(默认是8次),反复执行上述过程8次。注意:假设要回收400个Region,如果受限200ms,每次只能回收50个Region,反复8次刚好全部回收完毕。这么做的好处是避免单次停顿回收STW时间太长。
‐XX:G1HeapWastePercent=5 (默认是5%)
。
‐XX:G1MixedGCLiveThresholdPercent=85 (默认值85%)
。回收Region的时候,必须是存活对象低于85%。原文链接:https://blog.csdn.net/xue_ningmeng/article/details/118806117
作者:想要飞翔的天使
链接:http://www.pythonpdf.com/blog/article/332/31c604e51267ea239b2f/
来源:编程知识网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!