JVM is the basis of running java programs. Events in JVM such as GC, class loading, JPMS, heap, threads and so on can actually be logged.Through these logs, we can monitor events in the JVM and, in turn, tune java applications.
The Xlog log log service introduced in JDK9 was created for this purpose.Also note: Many people will inevitably encounter various problems when learning advanced Java.For this, I built a skirt 783802103, in which many architects communicate with each other, so don't go in without a foundation!
Through xlog, JDK unifies all kinds of events in JVM and outputs them out in a unified form.Subsystems are distinguished by tag parameters, emergency events are distinguished by log level, and output addresses are configured by logging output.
More please visit www.flydean.com
Use of xlog
Let's start with the simplest example of using 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).
The log is very long, not all of it is listed here.From the output log, we can see that the JVM performs many operations in the java-verson command.
We can see that the log lists the time spent on each operation, the log level, and the classification to which the operation belongs.
Through these logs, we can have a better understanding of how the JVM works.
Using Java -Xlog:helpCommand us to look at the basic format of 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 represent exactly what information needs to be output.Is expressed as tag=level.
Tags represent events or subsystems in the 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 represent the level of logging:
Available log levels: off, trace, debug, info, warning, error
Here is an example:
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 indicates where to output the log.
stdout for standard output and stderr for standard error.File means output to a file.
java -Xlog:all=debug:file=debug.log -version
decorators indicate what is output to the log.
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
Look at this example:
java -Xlog:gc*=debug:stdout:time,uptimemillis,tid -version
[2020-05-05T16:12:06.871-0800][32ms] Heap region size: 1M [2020-05-05T16:12:06.871-0800][32ms] Minimum heap 8388608 Initial heap 134217728 Maximum heap 2147483648 [2020-05-05T16:12:06.872-0800][33ms] Heap address: 0x0000000780000000, size: 2048 MB, Compressed Oops mode: Zero based, Oop shift amount: 3 [2020-05-05T16:12:06.872-0800][33ms] ConcGCThreads: 1 offset 8 [2020-05-05T16:12:06.872-0800][33ms] ParallelGCThreads: 4
xlog is a very useful feature provided in JDK9.You can use it in your daily work.In addition, you should note that many people will inevitably encounter various problems during the advanced learning process of Java.For this, I built a skirt 783802103, in which many architects communicate with each other, so don't go in without a foundation!
The text and pictures in this article are from the Internet plus your own ideas. They are for learning and communication purposes only. They do not have any commercial use. Copyright is owned by the original author. If you have any questions, please contact us in time for processing.