JVM 核心技术 调优分析与面试经验
生活随笔
收集整理的這篇文章主要介紹了
JVM 核心技术 调优分析与面试经验
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.GC分析類
import java.util.Random; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.LongAdder; /* 演示GC日志生成與解讀 */ public class GCLogAnalysis {private static Random random = new Random();public static void main(String[] args) {// 當(dāng)前毫秒時間戳long startMillis = System.currentTimeMillis();// 持續(xù)運行毫秒數(shù); 可根據(jù)需要進(jìn)行修改long timeoutMillis = TimeUnit.SECONDS.toMillis(1);// 結(jié)束時間戳long endMillis = startMillis + timeoutMillis;LongAdder counter = new LongAdder();System.out.println("正在執(zhí)行...");// 緩存一部分對象; 進(jìn)入老年代int cacheSize = 2000;Object[] cachedGarbage = new Object[cacheSize];// 在此時間范圍內(nèi),持續(xù)循環(huán)while (System.currentTimeMillis() < endMillis) {// 生成垃圾對象Object garbage = generateGarbage(100*1024);counter.increment();int randomIndex = random.nextInt(2 * cacheSize);if (randomIndex < cacheSize) {cachedGarbage[randomIndex] = garbage;}}System.out.println("執(zhí)行結(jié)束!共生成對象次數(shù):" + counter.longValue());}// 生成對象private static Object generateGarbage(int max) {int randomSize = random.nextInt(max);int type = randomSize % 4;Object result = null;switch (type) {case 0:result = new int[randomSize];break;case 1:result = new byte[randomSize];break;case 2:result = new double[randomSize];break;default:StringBuilder builder = new StringBuilder();String randomString = "randomString-Anything";while (builder.length() < randomSize) {builder.append(randomString);builder.append(max);builder.append(randomSize);}result = builder.toString();break;}return result;} }2.編譯執(zhí)行GC分析類
javac -encoding utf-8 GCLogAnalysis.java java GCLogAnalysis 正在執(zhí)行... 執(zhí)行結(jié)束!共生成對象次數(shù):7746在控制臺打印日志信息
java -XX:+PrintGCDetails GCLogAnalysis 正在執(zhí)行... [GC (Allocation Failure) [PSYoungGen: 49152K->7663K(56832K)] 49152K->17184K(186880K), 0.2004381 secs] [Times: user=0.00 sys=0.06, real=0.22 secs] [GC (Allocation Failure) [PSYoungGen: 56750K->7674K(105984K)] 66271K->33109K(236032K), 0.0088254 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] [GC (Allocation Failure) [PSYoungGen: 105955K->7661K(105984K)] 131391K->66334K(236032K), 0.0156307 secs] [Times: user=0.02 sys=0.03, real=0.01 secs] [GC (Allocation Failure) [PSYoungGen: 105965K->7677K(204288K)] 164638K->105454K(334336K), 0.0168545 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] [Full GC (Ergonomics) [PSYoungGen: 7677K->0K(204288K)] [ParOldGen: 97776K->96280K(201728K)] 105454K->96280K(406016K), [Metaspace: 2713K->2713K(1056768K)], 0.0196829 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] [GC (Allocation Failure) [PSYoungGen: 196608K->7678K(204288K)] 292888K->162201K(406016K), 0.0207846 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] [Full GC (Ergonomics) [PSYoungGen: 7678K->0K(204288K)] [ParOldGen: 154522K->146814K(294912K)] 162201K->146814K(499200K), [Metaspace: 2713K->2713K(1056768K)], 0.0316279 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] [GC (Allocation Failure) [PSYoungGen: 196017K->57322K(370688K)] 342832K->204136K(665600K), 0.0176191 secs] [Times: user=0.02 sys=0.02, real=0.02 secs] [GC (Allocation Failure) [PSYoungGen: 370666K->78847K(413696K)] 517480K->288396K(708608K), 0.0427564 secs] [Times: user=0.05 sys=0.13, real=0.04 secs] [GC (Allocation Failure) [PSYoungGen: 413175K->123891K(594944K)] 622724K->367967K(889856K), 0.0552594 secs] [Times: user=0.08 sys=0.11, real=0.06 secs] [Full GC (Ergonomics) [PSYoungGen: 123891K->0K(594944K)] [ParOldGen: 244076K->265452K(435200K)] 367967K->265452K(1030144K), [Metaspace: 2713K->2713K(1056768K)], 0.0547824 secs] [Times: user=0.16 sys=0.02, real=0.06 secs] 執(zhí)行結(jié)束!共生成對象次數(shù):5450 HeapPSYoungGen total 594944K, used 118753K [0x0000000780b80000, 0x00000007b6800000, 0x00000007c0000000)eden space 471040K, 25% used [0x0000000780b80000,0x0000000787f78658,0x000000079d780000)from space 123904K, 0% used [0x00000007a7480000,0x00000007a7480000,0x00000007aed80000)to space 160768K, 0% used [0x000000079d780000,0x000000079d780000,0x00000007a7480000)ParOldGen total 435200K, used 265452K [0x0000000702200000, 0x000000071cb00000, 0x0000000780b80000)object space 435200K, 60% used [0x0000000702200000,0x000000071253b0b0,0x000000071cb00000)Metaspace used 2719K, capacity 4486K, committed 4864K, reserved 1056768Kclass space used 297K, capacity 386K, committed 512K, reserved 1048576K總結(jié)
以上是生活随笔為你收集整理的JVM 核心技术 调优分析与面试经验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 批量插入SQL数据
- 下一篇: 02 | 服务治理:Nacos 如何实现