Y said

Flowers in Hangzhou have come into bloom recently. Originally planned to go to prince Prince Bay to see tulips, but arrived at the place only to find prince Prince Bay is overcrowded, can not enter without an appointment. So I wandered around the west Lake and took some pictures of flowers and plants.

Of course, in order to take beautiful photos, I dug out the long-lost SONY micro single camera. Besides, I also learned a wave of basic photography knowledge and organized it into a mind map, including the theoretical basis of photography and the main use method of SONY camera. If you are interested, you can follow my wechat official account “Made a Journey” and reply to “Photography Basics” to get the link to this mind map.

Why write this article

As a Java programmer, you have more or less experienced JVM and GC algorithms. But you don’t always have the opportunity to encounter and solve GC problems yourself at work.

The fundamentals of the JVM, the ideas and tools for GC troubleshooting, may not be used for a long time, but it is very important when it is needed.

During the entry interview, the interviewer will often ask: Do you have the experience to check GC problems? If you can talk about how to troubleshoot GC problems, regardless of whether you have actually checked GC problems or not, it is definitely a plus.

Recently, I checked a GC problem, so I want to sort out and summarize the knowledge points related to GC, and form a systematic knowledge system and thinking. I plan to write several articles about GC. Let’s start by introducing some tools for GC troubleshooting.

I have previously written about the basics of the JVM on my website yasinshaw.com. Interested students can search for “JVM” on the “Articles” page.

Logging and Monitoring

You can print the GC log of the program running by adding some command parameters to the Java program startup.

-xx :+PrintGC prints GC logs -xx :+PrintGCDetails Prints GC logs -xx :+PrintGCTimeStamps Prints GC timestamps (in the form of baseline time) -xx :+PrintGCDateStamps -xx :+PrintHeapAtGC Prints heap information before and after GC -Xloggc:.. /logs/gc.log Output path of the log fileCopy the code

Some commands may work in different Java versions. In Java 11, for example, it Will prompt you: -xx :+PrintGC is deprecated. Will use -xlog: GC instead.

The output log format is slightly different depending on the Java version and GC, depending on the Java version and GC on your project.

It is recommended that GC logs be integrated with Logback to take advantage of logback’s log cutting capabilities.

Coupled with visual monitoring tools and alarm tools, you can know the status and trend data of the JVM in real time. The open source solution recommended is Prometheus (using Jmx_EXPORTER) + Grafana.

Command line tool

Logs and monitoring are used to feedback historical data and monitor alarms. And historical data alone can sometimes be a poor guide to locating problems. With some command-line tools, you can get real-time JVM data.

JDK built-in command line tools

jps

JPS can view the machine running the Java process id, although use ps – ef | grep Java can achieve a similar effect, but it is more convenient to use the JPS.

Once you have the Java process ID, you can easily use the following command to get more information.

jstat

Using the process number obtained by JPS, you can use Jstat to get real-time information about regions in the JVM.

Below is a Java 8 program printed using Jstat. The last two parameters in the figure, 1000, are how often data is collected in milliseconds. 5 is the total number of collections.

Meaning of each column:

S0C, S1C, S0U, and S1U: Survivor 0/1 Capacity and usage EC, EU: Eden Capacity and usage OC, OU: Capacity and usage of the elderly generation PC, PU: Capacity and usage of the permanent generation YGC, YGT: Young GC count and GC duration FGC and FGCT: Full GC count and Full GC duration GCT: total GC durationCopy the code

jmap

Jmap is quite powerful. It can generate dump files, view statistics on sample heap objects, view ClassLoader information and Finalizer queues, and even perform remote diagnostics.

To see information about the JVM, use the Jmap-heap process number. Using Jmap is also more intuitive than using Jstat.

The debug package must be installed on the machine, and the version must be the same as the JDK version (including minor versions), or an error will be reported.

Arthas

Arthas is an open source Java diagnostics tool from Ali that developers love.

To start Arthas, use the following two commands:

curl -O https://arthas.aliyun.com/arthas-demo.jar
java -jar arthas-boot.jar
Copy the code

Arthas has two main commands for viewing JVM information. One is dashboard and the other is JVM.

The dashboard command allows you to view the overall information about your program and machine. This includes the main thread information, memory usage of various JVM areas, Java and operating system version information, and so on. And dashboard can also refresh in real time, dynamic monitoring.

There is a little Easter egg here, when we create our own threads, the priority can only be set to 1~10, but the JVM starts several threads (such as compilation threads, etc., can be set to -1).

// Thread setPriority method source
public final void setPriority(int newPriority) {
    ThreadGroup g;
    checkAccess();
    if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
        throw new IllegalArgumentException();
    }
    if((g = getThreadGroup()) ! =null) {
        if(newPriority > g.getMaxPriority()) { newPriority = g.getMaxPriority(); } setPriority0(priority = newPriority); }}Copy the code

Using JVM commands, you can get more detailed JVM information. This includes class loading, compilers, GC, memory management, operating systems, threads, and more.

In general, I personally find Arthas to be better than the command-line tools that come with the JDK. The information is more complete, and the layout looks more convenient.

Dump analysis

dump

Dump refers to generating snapshots. In Java, heap dump and Thread dump are the main methods of dump.

Java Heap dump can dump current JVM information into a binary file for later analysis. You can use arguments to dump Java programs at startup time, such as:

-xx :+HeapDumpBeforeFullGC: implements dump before FullGC. -xx :+HeapDumpAfterFullGC: implements dump after FullGC. OOM - XX: + HeapDumpOnOutOfMemoryError happen when the dump - XX: + HeapDumpPath: setting dump save pathCopy the code

You can also use the jmap tool mentioned above to manually dump the current information:

jmap -dump:live,file=heap.bin PID
Copy the code

Tips: Thread dump can be done using JStack, but it is not covered in this article.

Java’s Jconsole also has dump functionality to monitor local or remote Java processes. It has a graphical interface and allows you to view the JVM’s memory, threads, and more in real time.

Analysis of the

The JDK comes with dump file analysis tools, such as Jhat, that can analyze dump files and start an HTTP server. But Jhat has been removed in Java 9. It is recommended to use Visual VM to analyze dump files. Visual VM can graphically view object information and thread information during dump.

conclusion

These tools are used to troubleshoot GC problems. Before a GC problem occurs, you can log and monitor alarms to know the status of the JVM in the recent period as soon as possible.

In this case, you can use a command line tool such as arthas to analyze the current JVM situation if it is not urgent (for example, if GC becomes frequent but does not affect the business). If the Full GC takes a long time and services are affected, you can use the dump tool to dump the current data, roll back or limit the flow of the application, and analyze the dump file later.

And a support

My name is Yasin, a blogger who insists on original technology. My wechat official account is: Made a Program

All see here, if feel my article write also ok, might as well support once.

The first article will be sent to the public account, the best reading experience, welcome your attention.

Your every retweet, attention, like, comment is the biggest support for me!

There are learning resources, and frontline Internet companies within the push oh