View the resource usage of Java programs through Jconsole on Win (a tutorial summary, one article is enough)

Recently, I need to read a large file. In order to judge whether it has been read into the memory, I need a tool that can check the memory occupied by the jar package. After facing Baidu, I found the tool Jconsole that comes with jdk, and shared the tutorial with everyone.

1. Introduction

JConsole is a built-in Java performance analyzer, Java 5 began to introduce JConsole. Can be run from the command line or in a GUI shell. You can easily use JConsole to 监控 Java 应用程序性能and 跟踪 Java 中的代码.

The Jconsole tool checks the resource occupation requests of the program. When installing jdk, there is jconsole.exe tool in the bin directory, or through Win + R, enter jconsole.exe to start jconsole (you can enter it directly from the command line jconsole)

The advantage of this tool is that it takes up less system resources, and combined with Jstat, it can effectively monitor the changes of java memory and the reasons for the changes. Useful when tracking down memory leaks in a project.

Two, start


insert image description here

3. Connection

When the profiler pops up (depending on the version of Java being run and how many Java programs are running), a dialog may appear asking for a process URL to connect to, and may also list many different native Java processes (sometimes contains the JConsole process itself) to connect. As shown in the figure:
insert image description here
Double-click the process if you want to analyze which program.
insert image description here
insert image description here

At this point, you can see the resource usage and realize the most basic functions. If you want to know 远程连接, and 界面详情, you can look down !insert image description here

1. Set the JAVA program to be connected and analyzed by JConsolse when it is running

  1. A local program (compared to the computer that starts JConsole) can be connected to the locally started JConsole without setting any parameters (Java SE 6 does not need to be set, and before it still needs to set the runtime parameter -Dcom.sun.management.jmxremote)
  2. No authentication connection (the following setting means: the connection port is 8999, and it can be connected without authentication
  3. 参考:Monitoring and Management Using JMX Technology - Java SEMonitoring and Management Guide

4. Interface display

After connecting to the Java application, you can view the overview of the application, as shown in the figure. The four line graphs in the figure show the usage of the heap memory, the number of threads in the system, the number of loaded classes, and the usage of the CPU.

Generally, the main focus is on 内存栏the , 线程栏, 类栏, 概述栏and VMcolumns, MBeansand the columns are used when tracking specific issues.

It is worth mentioning that 点击右键you can 保存数据到CSVfile against the graph, and you can use other tools to analyze these data in the future.

You can disconnect or reconnect to a running Java Virtual Machine at any time using JConsole's green connection status icon in the upper right corner. Select Connection from the drop-down menu, then create a new connection, you can connect to any number of running Java Virtual Machines at the same time.

insert image description here

1. Memory monitoring

In JConsole, you can view the detailed information of the heap, including the size of the heap, the usage rate, the size of the eden area, the size of the survivor area, and the size of the permanent area.

Switch to “内存”the tab, JConsole can display the detailed information of the current memory. This includes not only the overall information of the heap memory, but also the usage of the eden area, the survivor area, and the old generation, as well as the usage of the non-heap area, that is, the permanent generation. Click the "Perform GC" button in the upper right corner to force the application to perform a FullGC operation, as shown in the figure.
insert image description here
Heap and non-heap memory

The Java virtual machine manages two kinds of memory: heap and non-heap memory, both of which are created when the Java virtual machine starts.

  1. Heap memory is the runtime data area where all class instances and arrays of the Java VM allocate memory. May be a fixed or variable size heap.
  2. Non-heap memory includes methods that are shared between all threads and the JVM internally for processing or optimization. It stores the class structure, running constant pool, field and method data, and the code of methods and constructors. The method area is logically part of the heap, depending on the specific implementation. Depending on the implementation, the Java Virtual Machine may not perform garbage collection or compaction. Like heap memory, the method area may be of a fixed or variable size. The memory in the method area does not need to be contiguous.
  3. In addition to the method area, the Java virtual machine may need to perform internal processing or optimization, which also belongs to the memory of non-heap memory. For example, just-in-time (JIT) compilers require memory for storing machine code translated from Java Virtual Machine's high-performance code.

2. Thread monitoring

Using JConsole, you can easily check the thread information in the system, and quickly locate the deadlock problem.

The Threads tab in JConsole allows developers to monitor threads within a program, as shown. JConsole shows the number of threads in the system, and shows all threads in the program at the bottom of the screen. Click the thread name to view the thread's stack information. (If there are too many threads, you can enter a string in the filter bar below to filter out the threads you want to observe)

统计图显示的是线程数目的峰值(红色)和当前活动的线程(蓝色)。

The Threads tab provides several useful operations.

findMonitorDeadlockedThreads: Detects if any thread object monitor lock is deadlocked. This operation returns an array of deadlocked thread IDs.
getThreadInfo: Returns thread information. This includes the name, stack trace and monitor lock, which thread is currently blocked, if any, which thread holds the lock, and thread contention statistics.
getThreadCpuTime: Returns the CPU time consumed by a given thread
insert image description here

Click the " 检测死锁" button to automatically detect deadlocks in multi-threaded applications.

3. Class loading

The "Classes" tab of JConsole, as shown in the figure, shows the number of classes that the system has loaded, and in the "Detailed Information" column, it also shows the number of classes that have been unloaded.

insert image description here

4. Virtual machine information

The VM summary shows the basic information of the current Java application, such as virtual machine type, virtual machine version, system thread information, operating system memory information, heap information, garbage collector type, JVM parameters, and class path.

In the "VM Summary" tab, JConsole displays the running environment of the current application, including the virtual machine type, version, heap information, and virtual machine parameters, as shown in the figure.
insert image description here

5. MBean management

Through JConsole, you can manage Mbeans in Java applications in a unified manner.

The MBean tab allows management of MBeans through JConsole, including viewing or setting attributes of MBeans, and running methods of MBeans. The figure shows the management interface of MBean, where the Verbose attribute of Memory is selected. By modifying the attribute value of Verbose, the output information of the GC operation can be dynamically turned on or off when the program is running.
insert image description here
There are many types of MBeans, and their functions are relatively powerful. Here are a few commonly used MBean operations.
insert image description here

6. Using plugins

In addition to basic functions, JConsole also supports plug-in extensions. There is a built-in JConsole plug-in in the JDK installation directory, which is located under %JAVA_HOME%\demo\management\JTop. Use the following command to make JConsole load the plug-in and start it:
insert image description here
After JConsole starts, connect to any Java application, and you can enter the JTop page, as shown in the figure.

The JTop plug-in sorts by CPU time, and displays the thread with the longest CPU time at the top of the table. Through this plug-in, developers can quickly find the name of the thread that takes up the longest CPU time, and locate the thread code through the thread snapshot.
insert image description here

The complete source code of the JTop plug-in can be found in the JDK installation directory. Interested readers can modify the source code of JTop to display more thread information.

reference link

  1. Detailed explanation of JConsole: https://blog.csdn.net/D420941934/article/details/120473194
  2. View program resource usage through Java: https://www.jb51.cc/java/3556429.html
  3. Use of jconsole tool: https://www.cnblogs.com/kongzhongqijing/articles/3621441.html

Guess you like

Origin blog.csdn.net/qq_43961619/article/details/130575726