The application was developed in the Java language, although the JVM did the GC collection and cleaning for us; However, if not used properly, it will still lead to some objects resident in the heap space can not be cleared by the garbage collector, resulting in memory leaks, memory leaks, etc. Today we review some common and useful tools for memory leak analysis and GC analysis in projects.

0x01: JDK comes with tools

The JDK itself comes with a large number of monitoring and analysis tools to deal with memory leaks, including the following:

  • JPS: You can view all Java processes running in the current system
  • Jstat: View GC status for a specific Java process
  • Jmap: View heap memory usage of a Java process
  • Jvisualvm: Visually view heap memory vs. Metaspace usage
  • Jstack: View the stack of threads running on a particular Java

0x02: Eclipse Memory Analyzer

The official website has the following English introduction:

The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption.

Use the Memory Analyzer to analyze productive heap dumps with hundreds of millions of objects, quickly calculate the retained sizes of objects, see who is preventing the Garbage Collector from collecting objects, run a report to automatically extract leak suspects.

The general idea is as follows:

Eclipse Memory Analyzer (MAT for short) is a fast and feature-rich Java heap Analyzer that helps you find Memory leaks and reduce Memory consumption. Memory Analyzer is used to analyze efficient heap dumps with hundreds of millions of objects, quickly calculate the retention size of objects, see who is preventing the garbage collector from collecting objects, and run reports to automatically extract suspected leaks.

Website address: https://www.eclipse.org/mat/Copy the code

This tool is great for analyzing memory leaks, graphically showing memory snapshots printed with the Jmap command, and is free.

0 x03: JProfiler

JProfiler is a commercially available tool for checking and tracking the performance of systems (limited to Java development). JProfiler can monitor JVM health and performance by monitoring system memory usage in real time, monitoring garbage collection, thread health, and more. Dedicated to analyzing J2SE and J2EE applications. It combines CPU, thread, and memory profiling into one powerful application. JProfiler provides a number of plug-ins for IDE integration and application server integration. JProfiler visualization allows users to find performance bottlenecks, catch memory leaks, and solve thread problems.

Website address: https://www.ej-technologies.com/products/jprofiler/overview.htmlCopy the code

The only downside to JProfiler is the need to buy copyright; But the functionality is still great.

0 x04: Arthas

First of all, Ali makes the best products, and Arthas is certainly very useful.

Arthas is an open source Java online diagnostic tool from Alibaba that is very powerful and can solve many problems that are not easily solved online. Arthas diagnostics use command line interactive mode, support JDK6+, Linux, Mac, And Windows operating systems, and support TAB key auto-completion of various types of information, making diagnostics very convenient.

Code warehouse address: https://github.com/alibaba/arthasCopy the code

From the Arthas website you can see that Arthas has the following characteristics

  • Check whether or where a class is loaded. (Useful for resolving JAR file conflicts)
  • Decompile a class to ensure that the code works as expected.
  • View class loader statistics, such as the number of class loaders, the number of classes loaded by each class loader, class loader hierarchy, possible class loader leaks, etc.
  • View method call details, such as method parameters, returned objects, exceptions thrown, and so on.
  • Checks the stack trace for the specified method call. This is useful when a developer wants to know the caller of the method.
  • Trace method calls to find lachyon calls.
  • Monitor method call statistics, such as QPS, RT, success rate, and so on.
  • Monitor system metrics, thread status and CPU usage, GC statistics, and more.
  • The command line interaction mode is supported and the auto-complete function is enabled.
  • Telnet and Websocket are supported to enable local and remote diagnostics from the command line and browser.
  • Support analyzer/flame map
  • Support for JDK 6+.
  • Supports Linux, Mac, and Windows.

0x05: Operating system command

After all, the operating system’s own commands are fundamental. Java can also be checked and checked to a certain extent by Linux’s own commands. The commands may vary with Linux versions. The following uses CentOS as an example:

  • Free: Check the memory usage and free memory
  • Top: monitors the memory, CPU, and I/O of all processes in real time
  • Lsof: The lsof command has many functions, including the function to check the status of file handles opened by a process
  • The Linux operating system has many commands. You can find related commands to monitor the memory, I/O, network, and disk information. Specific scenarios to find relevant information for relevant processing.

0 x06: GCViewer

GCViewer is a practical GC log analysis software. It is free and open source, but you need to install the JDK or Java environment to use it, because it is developed in the Java language. The software provides powerful functional support for GC log analysts, greatly improving the efficiency of GC log analysis.

Website address: https://www.tagtraum.com/gcviewer.htmlCopy the code