示例代码-2
// 启动程序,模拟用户请求 // 每100毫秒钟创建1000线程,每个线程创建一个512kb的对象,最多100毫秒内同时存在1000线程,并发量1000/s,吞吐量6000/s,查看GC的情况 @SpringBootApplication public class PerformanceApplication { public static void main(String[] args) { SpringApplication.run(PerformanceApplication.class, args); Executors.newScheduledThreadPool(1).scheduleAtFixedRate(() -> { new Thread(() -> { for (int i = 0; i < 1000; i++) { try { // 不干活,专门512kb的小对象 byte[] temp = new byte[1024 * 512]; Thread.sleep(new Random().nextInt(100)); // 随机睡眠200毫秒秒以内 } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); }, 100, 100, TimeUnit.MILLISECONDS); } } // 打包 mvn clean package // 服务器上运行 performance-1.1.0.jar // 对象存活时间短,处理量大,属于响应时间优先
GC调优
# 实时监控:jstat -gc -h10 $(jcmd | grep "performance-1.1.0.jar" | awk '{print $1}') 1000 # Parallel GC 服务器默认 java -Xmx1024m -Xloggc:/netease/gc6.log -jar performance-1.1.0.jar UseAdaptiveSizePolicy自适应默认开启,所以Eden区会自动变化大小 S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 37376.0 37888.0 34817.1 0.0 272384.0 92500.6 125952.0 57532.9 35120.0 33541.2 4656.0 4335.4 286 2.698 7 0.360 3.059 36864.0 36864.0 32289.0 0.0 275456.0 0.0 125952.0 87877.8 35120.0 33541.2 4656.0 4335.4 300 2.812 7 0.360 3.173 35328.0 35840.0 31777.0 0.0 276480.0 0.0 128000.0 61118.4 35120.0 33542.5 4656.0 4335.4 314 2.924 8 0.404 3.328 34816.0 34816.0 31297.0 0.0 279552.0 139082.4 128000.0 102215.6 35120.0 33555.8 4656.0 4335.4 328 3.029 8 0.404 3.433 33792.0 33792.0 30752.9 0.0 281600.0 181082.7 130048.0 68303.1 35120.0 33555.8 4656.0 4335.4 342 3.138 9 0.448 3.586 33280.0 33280.0 0.0 29728.9 282624.0 132715.1 130048.0 122168.7 35120.0 33555.8 4656.0 4335.4 357 3.254 9 0.448 3.701 33280.0 33280.0 25152.8 0.0 282624.0 78765.8 132608.0 98078.6 35120.0 33556.9 4656.0 4335.4 372 3.361 10 0.490 3.851 31232.0 27136.0 0.0 26688.8 285696.0 136307.1 136704.0 76004.6 35120.0 33558.1 4656.0 4335.4 387 3.479 11 0.542 4.021 31744.0 31232.0 26656.8 0.0 286208.0 0.0 138752.0 49337.7 35120.0 33558.1 4656.0 4335.4 402 3.595 12 0.585 4.180 29184.0 29184.0 25152.8 0.0 289792.0 0.0 138752.0 112435.6 35120.0 33558.1 4656.0 4335.4 418 3.716 12 0.585 4.301 默认情况,实时监控结果:10秒内132次YGC,5次FullGC,单词YGC耗时0.008s,总耗时1.242秒 1、 调大-XX:ParallelGCThreads=4 java -Xmx1024m -Xloggc:/netease/gc7.log -XX:ParallelGCThreads=4 -jar performance-1.1.0.jar S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 47616.0 48128.0 43073.3 0.0 252416.0 227997.7 130560.0 122261.9 35120.0 33506.0 4656.0 4334.8 197 2.479 6 0.359 2.838 47104.0 47104.0 43105.3 0.0 254976.0 153173.5 116736.0 67205.9 35120.0 33506.0 4656.0 4334.8 208 2.629 7 0.405 3.034 46080.0 46080.0 44097.3 42529.3 257024.0 0.0 116736.0 86822.5 35120.0 33506.0 4656.0 4334.8 221 2.767 7 0.405 3.172 45568.0 45568.0 0.0 42049.3 258048.0 129250.1 116736.0 104391.0 35120.0 33506.0 4656.0 4334.8 233 2.933 7 0.405 3.338 44544.0 44544.0 0.0 43041.3 260096.0 27359.1 120320.0 68802.0 35120.0 33506.0 4656.0 4334.8 245 3.111 8 0.463 3.57 44544.0 44544.0 39937.2 0.0 260096.0 51577.7 120320.0 97642.8 35120.0 33506.0 4656.0 4334.8 258 3.283 8 0.463 3.746 44544.0 36864.0 0.0 36353.1 261120.0 7862.2 121856.0 68276.2 35120.0 33506.0 4656.0 4334.8 271 3.439 9 0.508 3.947 42496.0 42496.0 0.0 37441.1 263680.0 0.0 121856.0 106301.3 35120.0 33506.0 4656.0 4334.8 285 3.654 9 0.508 4.162 40960.0 41472.0 37889.2 0.0 265216.0 0.0 122880.0 85228.3 35120.0 33506.0 4656.0 4334.8 298 3.834 10 0.551 4.385 41472.0 41472.0 37441.1 0.0 266240.0 0.0 123904.0 57506.1 35120.0 33515.1 4656.0 4334.8 312 4.010 11 0.595 4.605 实时监控结果:10秒内115次GC,5次fullGC,总耗时1.767,单次YGC时间0.014s 多线程,也不管用 2、 降低耗时,设置-XX:MaxGCPauseMills=5 java -Xmx1024m -Xloggc:/netease/gc8.log -XX:MaxGCPauseMillis=5 -jar performance-1.1.0.jar S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 11264.0 12800.0 0.0 0.0 50688.0 0.0 48128.0 28445.5 35200.0 33483.7 4736.0 4334.6 22 0.136 3 0.184 0.320 26624.0 25088.0 0.0 18496.6 44032.0 11192.5 48128.0 32045.6 35200.0 33483.7 4736.0 4334.6 29 0.165 3 0.184 0.349 33792.0 34304.0 21504.7 0.0 39424.0 11034.2 48128.0 36789.8 35200.0 33483.7 4736.0 4334.6 40 0.218 3 0.184 0.401 34304.0 34816.0 27712.8 0.0 37888.0 0.0 64512.0 44331.6 35200.0 33483.9 4736.0 4334.6 54 0.296 5 0.266 0.561 32768.0 26624.0 0.0 0.0 37888.0 0.0 76288.0 49968.7 35200.0 33483.9 4736.0 4334.6 69 0.385 9 0.429 0.815 47616.0 48640.0 39489.2 0.0 28672.0 0.0 84992.0 54068.5 35200.0 33483.9 4736.0 4334.6 92 0.543 11 0.513 1.056 57856.0 57856.0 0.0 19968.6 20480.0 9283.9 100352.0 59194.0 35200.0 33484.9 4736.0 4334.6 123 0.764 15 0.682 1.446 35840.0 36864.0 13312.4 0.0 13824.0 0.0 115200.0 88901.4 35200.0 33487.8 4736.0 4335.7 172 1.076 26 1.113 2.188 24576.0 17408.0 0.0 16896.5 9216.0 0.0 99840.0 82761.6 35200.0 33488.2 4736.0 4335.7 227 1.363 40 1.757 3.121 14848.0 14848.0 9216.3 0.0 6144.0 0.0 95744.0 84799.3 35200.0 33488.5 4736.0 4335.7 310 1.657 55 2.399 4.056 实时监控结果:结果不太好,不合适。 # CMS 3、 改用CMS回收器 java -Xmx1024m -Xloggc:/netease/gc9.log -XX:+UseConcMarkSweepGC -jar performance-1.1.0.jar S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 2048.0 2048.0 1536.0 0.0 16384.0 0.0 848412.0 836821.7 35220.0 33468.3 4756.0 4331.8 116 0.804 7 0.037 0.841 17024.0 17024.0 16901.3 0.0 136320.0 91188.6 878208.0 75373.9 35220.0 33468.5 4756.0 4331.8 122 0.859 8 0.114 0.973 17024.0 17024.0 0.0 16901.3 136320.0 90524.2 878208.0 154741.3 35220.0 33468.5 4756.0 4331.8 127 0.900 8 0.114 1.015 17024.0 17024.0 0.0 16901.3 136320.0 67869.0 878208.0 279165.8 35220.0 33468.5 4756.0 4331.8 133 0.953 8 0.114 1.068 17024.0 17024.0 16898.9 0.0 136320.0 29055.4 878208.0 462985.0 35220.0 33469.5 4756.0 4331.8 140 1.020 8 0.114 1.134 17024.0 17024.0 0.0 16898.9 136320.0 32667.9 878208.0 681621.7 35220.0 33472.4 4756.0 4332.9 147 1.089 8 0.114 1.204 17024.0 17024.0 0.0 16902.3 136320.0 27188.4 878208.0 294018.9 35220.0 33472.8 4756.0 4332.9 155 1.174 10 0.128 1.302 17024.0 17024.0 16900.9 0.0 136320.0 0.0 878208.0 607890.7 35220.0 33473.4 4756.0 4332.9 164 1.266 10 0.128 1.394 17024.0 17024.0 0.0 16898.9 136320.0 80996.3 878208.0 225418.4 35220.0 33475.7 4756.0 4332.9 173 1.358 12 0.140 1.499 17024.0 17024.0 16898.9 0.0 136320.0 0.0 878208.0 607899.4 35220.0 33475.7 4756.0 4332.9 184 1.470 12 0.140 1.611 实时监控结果:高频回收,会抢占用户线程,根据实际需要进行调优 # G1 建议大堆使用 4、 改用G1 java -Xmx1024m -Xloggc:/netease/gc10.log -XX:+UseG1GC -jar performance-1.1.0.jar S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 0.0 0.0 0.0 0.0 55296.0 8192.0 993280.0 592928.3 35200.0 33534.8 4736.0 4331.4 272 3.725 0 0.000 3.725 0.0 0.0 0.0 0.0 55296.0 8192.0 993280.0 597028.5 35200.0 33534.8 4736.0 4331.4 284 3.988 0 0.000 3.988 0.0 0.0 0.0 0.0 55296.0 7168.0 993280.0 490485.7 35200.0 33535.9 4736.0 4331.4 295 4.299 0 0.000 4.299 0.0 0.0 0.0 0.0 55296.0 8192.0 993280.0 603137.7 35200.0 33537.1 4736.0 4331.4 308 4.568 0 0.000 4.568 0.0 0.0 0.0 0.0 641024.0 22528.0 407552.0 392447.4 35200.0 33537.1 4736.0 4331.4 319 4.869 0 0.000 4.869 0.0 0.0 0.0 0.0 55296.0 2048.0 993280.0 316671.2 35200.0 33537.1 4736.0 4331.4 330 5.151 0 0.000 5.151 0.0 0.0 0.0 0.0 55296.0 7168.0 993280.0 614372.8 35200.0 33537.1 4736.0 4331.4 342 5.408 0 0.000 5.408 0.0 0.0 0.0 0.0 449536.0 44032.0 599040.0 598790.9 35200.0 33537.1 4736.0 4331.4 355 5.715 0 0.000 5.715 0.0 0.0 0.0 0.0 55296.0 1024.0 993280.0 299773.5 35200.0 33537.1 4736.0 4331.4 367 6.045 0 0.000 6.045 0.0 0.0 0.0 0.0 55296.0 7168.0 993280.0 625419.5 35200.0 33537.1 4736.0 4331.4 380 6.335 0 0.000 6.335 实时监控结果:难看的数据 5、增加分区大小 java -Xmx1024m -Xloggc:/netease/gc11.log -XX:+UseG1GC -XX:G1HeapRegionSize=64m -jar performance-1.1.0.jar S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 0.0 65536.0 0.0 65536.0 327680.0 262144.0 229376.0 43625.6 35196.0 33417.9 4732.0 4335.1 84 0.899 0 0.000 0.899 0.0 65536.0 0.0 65536.0 327680.0 229376.0 229376.0 44153.1 35196.0 33417.9 4732.0 4335.1 89 0.944 0 0.000 0.944 0.0 65536.0 0.0 65536.0 327680.0 0.0 229376.0 43658.6 35196.0 33417.9 4732.0 4335.1 95 1.001 0 0.000 1.001 0.0 65536.0 0.0 65536.0 327680.0 196608.0 229376.0 43673.1 35196.0 33417.9 4732.0 4335.1 100 1.047 0 0.000 1.047 0.0 65536.0 0.0 65536.0 327680.0 131072.0 229376.0 43690.6 35196.0 33417.9 4732.0 4335.1 106 1.113 0 0.000 1.113 0.0 65536.0 0.0 65536.0 327680.0 196608.0 229376.0 43705.6 35196.0 33417.9 4732.0 4335.1 112 1.181 0 0.000 1.181 0.0 65536.0 0.0 65536.0 327680.0 98304.0 229376.0 44236.6 35196.0 33418.6 4732.0 4335.1 119 1.271 0 0.000 1.271 0.0 65536.0 0.0 65536.0 327680.0 131072.0 229376.0 43743.6 35196.0 33419.1 4732.0 4335.1 126 1.360 0 0.000 1.360 0.0 65536.0 0.0 65536.0 327680.0 196608.0 229376.0 44274.6 35196.0 33419.8 4732.0 4335.1 133 1.461 0 0.000 1.461 0.0 65536.0 0.0 65536.0 393216.0 196608.0 262144.0 43781.6 35196.0 33419.8 4732.0 4335.1 140 1.546 0 0.000 1.546
3、 结语
主要是演示切换的过程和思路,实际还是要结合系统情况、系统需要来调整。
1、 GC调优就是逐步调试的过程,对每个参数的含义了解后,再根据官方手册,一个个调试,找到符合应用的最佳配置点。是一个细致活,难度高。
2、 再重复一句,性能问题,98.75%上是业务代码上面。
3、 无监控,不调优。