JDK9的新特性:JVM的xlog如何正確運用?案例詳解


 

JDK9的新特性:JVM的xlog

JVM是java程序運行的基礎,JVM中各種事件比如:GC,class loading,JPMS,heap,thread等等其實都可以有日志來記錄。通過這些日志,我們可以監控JVM中的事件,並可以依次來對java應用程序進行調優。

在JDK9中引入的Xlog日志服務就是為這個目的而創建的。另外注意:很多人在學Java高級過程中難免會遇到各種問題解決不了。為此我建了個裙 783802103,里面很多架構師一起交流解答,沒基礎勿進哦!

通過xlog,JDK將JVM中的各種事件統一起來,以統一的形式對外輸出。通過tag參數來區分子系統,通過log level來區分事件的緊急性,通過logging output來配置輸出的地址。

更多內容請訪問www.flydean.com

xlog的使用

先看一個最簡單的xlog的使用例子:

java -Xlog -version

輸出結果:

[0.016s][info][os] Use of CLOCK_MONOTONIC is supported
[0.016s][info][os] Use of pthread_condattr_setclock is not supported
[0.016s][info][os] Relative timed-wait using pthread_cond_timedwait is associated with the default clock
[0.017s][info][os] SafePoint Polling address, bad (protected) page:0x0000000108901000, good (unprotected) page:0x0000000108902000
[0.022s][info][biasedlocking] Aligned thread 0x00007f983e008200 to 0x00007f983e008800
[0.023s][info][os,thread    ] Thread attached (tid: 10499, pthread id: 123145571979264).

日志非常非常長,這里就不全部列出來了。從輸出的日志我們可以看到java -verson命令中JVM執行了諸多的操作。

我們可以看到日志中對每一個操作都列出了操作花費的時間,日志級別和操作所屬的分類。

通過這些日志,我們對於JVM的運行可以有更加深入的理解。

使用java -Xlog:help命令我們看一下xlog的基本格式:

-Xlog Usage: -Xlog[:[selections][:[output][:[decorators][:output-options]]]]
	 where "selections" are combinations of tags and levels of the form tag1[+tag2...][*][=level][,...]
	 NOTE: Unless wildcard (*) is specified, only log messages tagged with exactly the tags specified will be matched.

selections

selections表示的是到底需要輸出哪些信息。是以tag=level來表示的。

tag表示的是JVM中的事件或者子系統:

Available log tags:
 add, age, alloc, annotation, aot, arguments, attach, barrier, biasedlocking, blocks, bot, breakpoint, bytecode, cds, census, class, classhisto, cleanup, codecache, compaction, compilation, constantpool, constraints, container, coops, cpu, cset, data, datacreation, dcmd, decoder, defaultmethods, director, dump, dynamic, ergo, event, exceptions, exit, fingerprint, free, freelist, gc, handshake, hashtables, heap, humongous, ihop, iklass, init, inlining, install, interpreter, itables, jfr, jit, jni, jvmti, liveness, load, loader, logging, malloc, mark, marking, membername, memops, metadata, metaspace, methodcomparator, mirror, mmu, module, monitorinflation, monitormismatch, nestmates, nmethod, normalize, numa, objecttagging, obsolete, oldobject, oom, oopmap, oops, oopstorage, os, pagesize, parser, patch, path, perf, periodic, phases, plab, preorder, preview, promotion, protectiondomain, ptrqueue, purge, record, redefine, ref, refine, region, reloc, remset, resolve, safepoint, sampling, scavenge, setting, smr, stackmap, stacktrace, stackwalk, start, startuptime, state, stats, streaming, stringdedup, stringtable, subclass, survivor, sweep, symboltable, system, table, task, thread, time, timer, tlab, tracking, unload, unshareable, update, verification, verify, vmmutex, vmoperation, vmthread, vtables, vtablestubs, workgang
 Specifying "all" instead of a tag combination matches all tag combinations

levels表示的是日志的級別:

Available log levels:
 off, trace, debug, info, warning, error

下面舉個例子:

java -Xlog:os,class=info -version

輸出結果:

[0.002s][info][os] Use of CLOCK_MONOTONIC is supported
[0.002s][info][os] Use of pthread_condattr_setclock is not supported
[0.002s][info][os] Relative timed-wait using pthread_cond_timedwait is associated with the default clock
[0.003s][info][os] SafePoint Polling address, bad (protected) page:0x0000000109543000, good (unprotected) page:0x0000000109544000
[0.006s][info][os] attempting shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libjava.dylib
[0.007s][info][os] shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libjava.dylib was successful
[0.007s][info][os] attempting shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libzip.dylib
[0.010s][info][os] shared library load of /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home/lib/libzip.dylib was successful

output

output表示將日志輸出到什么地方。

output的可選項:

 stdout/stderr
 file=<filename>

stdout表示標准輸出,stderr表示標准錯誤。file表示輸出到文件里面。

舉個例子:

java -Xlog:all=debug:file=debug.log -version

decorators

decorators表示輸出哪些內容到日志中。

time (t), utctime (utc), uptime (u), timemillis (tm), uptimemillis (um), timenanos (tn), uptimenanos (un), hostname (hn), pid (p), tid (ti), level (l), tags (tg)
 Decorators can also be specified as "none" for no decoration

看下這個例子:

 java -Xlog:gc*=debug:stdout:time,uptimemillis,tid -version

輸出結果:

 [2020-05-05T16:12:06.871-0800][32ms][9475] Heap region size: 1M
[2020-05-05T16:12:06.871-0800][32ms][9475] Minimum heap 8388608  Initial heap 134217728  Maximum heap 2147483648
[2020-05-05T16:12:06.872-0800][33ms][9475] Heap address: 0x0000000780000000, size: 2048 MB, Compressed Oops mode: Zero based, Oop shift amount: 3
[2020-05-05T16:12:06.872-0800][33ms][9475] ConcGCThreads: 1 offset 8
[2020-05-05T16:12:06.872-0800][33ms][9475] ParallelGCThreads: 4

總結

xlog是JDK9中提供的非常有用的一個功能。大家可以在日常的工作中使用。另外大家要注意:很多人在學Java高級過程中難免會遇到各種問題解決不了。為此我建了個裙 783802103,里面很多架構師一起交流解答,沒基礎勿進哦!

本文的文字及圖片來源於網絡加上自己的想法,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM