Use of JConsole-


1. Introduction

JConsoleis a Javamonitoring and management console tool that can be used to monitor Javathe performance and resource utilization of virtual machines (JVMs). It provides a graphical interface to view real-time JVMrunning status, memory usage, thread activity, garbage collection, and other information, as well as perform some management operations.

Using JConsolecan help developers and system administrators 性能调优、内存泄漏排查和故障诊断to improve application reliability and performance.

insert image description here

In short, JConsoleit is a powerful Javamonitoring and management tool that can help developers and system administrators monitor and manage Javaapplication performance and resource utilization in real time.


Two, use

If you don't have JDK installed, please refer to the blog:

Here are JConsolethe steps to use:

  1. Start JConsole: JConsole is a tool included in the JDK, so first make sure you have installed the JDK. Then, enter the jconsole command in the command line or double-click jconsole.exeto start JConsole.

  2. Connect to Java process: After starting JConsole, an interface will pop up, displaying all running Java processes. Select the Java process to monitor, and click 连接the button.

  3. Monitoring JVM: After a successful connection, JConsole provides multiple tabs for monitoring various JVM resources. Commonly used tabs include:

    • Overview: Displays overview information of the JVM, such as memory usage, number of threads, class loader information, etc.

    • Memory: Displays the memory usage of the JVM, including heap memory, non-heap memory, memory pool, etc.

    • Threads: Displays thread activity in the JVM, including the number of threads, thread status, deadlocks, and more.

    • Classes: Displays the class information loaded by the JVM, including the number of loaded classes, the number of unloaded classes, etc.

    • VM Summary (VM Summary): displays the basic information of the JVM, such as JVM startup parameters, command line parameters, etc.

    • MBean: Displays information about all MBeans registered on the platform.MBeanserver.

    • Perform operations: In addition to monitoring functions, JConsole can also perform some administrative operations. For example, operations such as garbage collection, thread dump, and thread deadlock detection can be performed through JConsole.

These operations can be performed by clicking the corresponding buttons.


2.1 Start JConsole

Under windows, we can directly find the directory where the JDK is located, go to /binthe directory to find it, jconsole.exedouble-click to run it

insert image description here

If you configure the Java environment correctly, win + Ropen 命令提示符the window and enter jconsolethe command can also be opened

insert image description here


2.2 Connecting to the Java process

After starting, you can see that there are two options on the interface, one is 本地进程and the other is远程进程

  • Local process: Compared with the computer that starts JConsole, it can be connected to the locally started JConsole without setting any parameters (no need to set after Java SE 6, and set runtime parameters before -Dcom.sun.management.jmxremote)
  • Remote process: Enter the remote connection address, the format is ip:port(note that port is the listening port, not the service port) and user name, password (if necessary) to connect to the monitoring interface.

2.2.1 Local connection

insert image description here

All running Java programs on the current computer will be displayed in the local process, just select and double-click to enter, and then click不安全的连接

insert image description here

You can enter the monitoring interface

insert image description here


2.2.2 Remote connection

The remote connection needs to go to the server, after adding the following startup parameters, restart the project

# 远程服务器ip,即本机ip,不建议用 localhost、127.0.0.1
-Djava.rmi.server.hostname=XXX.XXX.XXX.XXX 
# 允许 JMX 远程调用
-Dcom.sun.management.jmxremote
# 自定义 JMX 端口号
-Dcom.sun.management.jmxremote.port=8899
# 是否需要 ssl 安全连接方式
-Dcom.sun.management.jmxremote.ssl=false
# 是否需要密钥
-Dcom.sun.managent.jmxremote.authenticate=false

For example:

java 
-Dcom.sun.management.jmxremote.port=8899 
-Dcom.sun.managent.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false 
-jar xxx.jar

Note: This port number cannot be the same as the program port. It needs a separate, non-repeated port number, otherwise an 端口被占用error will be reported. Also, if the firewall of your server is not closed, you must develop this port in the firewall. Secondly, if you use If your server is a cloud server, you need to develop this port on the cloud server.

Secondly, you also need to create jmxremote.passworda file for 控制远程连接用户名和密码.

You can %JAVA_HOME%/jre/lib/managementcheck whether there is jmxremote.passworda file in the directory, if not, you can use jmxremote.password.templatethe file in the directory as a prototype to copy a

cp jmxremote.password.template jmxremote.password

then edit jmxremote.passwordthe file

vim jmxremote.password

Modify the bottom two lines and #remove the comment

# monitorRole  QED
# controlRole   R&D

insert image description here

monitorRoleIt is the viewing role (also the login user name, in accessthe file), has read-only permission, QEDand is the set password.
controlRoleIs the control user (also the login user name, in accessthe file), has read and write permissions, R&Dand is the set password.

Finally, authorize the file:

chmod 600 jmxremote.password

connect:

insert image description here

insert image description here

The connection is successful as shown in the figure:

insert image description here


2.3 Monitoring JVM resources

Overview:

After the connection is successful, JConsole will display the overview information of the application. In the overview tab, you can view the garbage collection, class loading, thread and other information of the application.

insert image description here

Memory:

Displays the memory usage of the JVM, including heap memory, non-heap memory, memory pool, etc.

insert image description here

内存The interface has 执行GCa button that can be clicked at any time to perform a garbage collection. At the same time, you can switch the memory area to be monitored by clicking 图表-下拉选项or clicking .右下角内存图

Java VMManages two kinds of memory: heap memory and non-heap memory, both of which are Java VMcreated at startup.

  • (1) Heap memory: It is the runtime data area, which will be created when the virtual machine starts, and it stores the data managed by the Automatic Storage Management System (Automatic Storage Management System, also known as the Garbage Collector garbage collector). Various objects that need not and cannot be explicitly destroyed. The capacity of the heap can be a fixed size, or it can be dynamically expanded according to the needs of program execution, and it will automatically shrink when too much space is not needed. The memory used by the heap does not need to be guaranteed to be continuous.

    • Memory model:
      insert image description here

    • Heap memory can be divided into two main areas:

      • Young/New Generation: Newborn objects are placed in the Young/New Generation, which consists of Edenand Survivor Space. EdenThe ratio of Survivor Spacethe space size to is by default 8:1, that is, when Young/New Generationthe total space size of the area is 10M , the space size of Eden is 8M, Survivor Spaceand the two blocks are allocated respectively 1M. This ratio can be -XX:SurvivorRatiomodified by the parameter. Young/New GenerationThe size of can -Xmnbe specified by the parameter.
        • Eden : Eden area. When an object is created, it is first placed in the Eden area. After garbage collection, objects that cannot be recycled are placed in an empty Survivorarea.
        • Survivor Space : The survivor area is used to save objects that have not been recycled after garbage collection in the Eden memory area. Survivor SpaceDivided into two areas of the same size, namely s0and s1, when triggered, Minor GCmove the surviving objects to s0(From Eden To s0), so that Eden is cleared and can be allocated to new objects; when triggered Minor GCagain , s0and Edenthe surviving objects in and are moved to s1(From s0 To s1), s0that is, they are emptied. At the same time, only Edenand one Survivor Spaceare operated at the same time, so at least one of the two areas of s0and will be free at the same time.s1Survivor
      • Old generation (Old/Tenured Generation): The old generation is used to store objects that survived several garbage collections in the program. It may also be that large objects that cannot be allocated in the new generation will directly enter the old generation, such as cached objects. The memory size occupied by the age is the difference between -Xmxand -Xmnthe two parameters.
    • By default, the ratio of the young generation (Young) to the old generation (Old) is 1:2(this value can be -XX:NewRatospecified by parameters), that is: the young generation (Young) = 1/3 of the heap memory space size, the old generation (Old ) = 2/3 the size of the heap space. Among them, the young generation (Young) is subdivided into Eden and two Survivor areas, and the default Eden:s0:s1 = 8:1:1 (can be set by parameter -XX:SurvivorRatio). The entire size of the heap memory = the size of the young generation + the size of the old generation. The default heap memory is 1/64 of the physical memory (<1GB). When the free heap memory is less than 40% by default, the JVM will increase the heap until the -Xmxmaximum The limit can MinHeapFreeRatiobe adjusted through the parameter; when the default free heap memory is greater than 70%, the JVM will reduce the heap to the -Xmsminimum limit, which can MaxHeapFreeRatiobe adjusted through the parameter.

  • (2) Non-heap memory: includes Java VMthe method area shared between all threads and memory required for internal processing or optimization, which stores each type of structure, such as runtime constant pool, field and method data, and methods and constructors The method area is logically part of the heap, but depending on the implementation, Java VMit may not be garbage collected or compressed. Like heap memory, the method area can be of fixed or variable size. The memory in the method area does not need to be continuously.

    • It can be divided into:
      • Metaspace: Metaspace is HotSpot jvmthe implementation of the method area in . The method area mainly stores class information, constant pool, method data, method code, etc. The method area logically belongs to a part of the heap, but in order to distinguish it from the heap, it is usually called again 非堆.
      • Code Cache: HotSpot Java VMAlso includes Code Cache, which contains memory used to compile and store native code.
      • Compressed Class Space: compressed class space

thread:

Displays thread activity in the JVM, including thread count, thread state, deadlocks, and more.

insert image description here

Red : peak thread count
Blue : active thread count

The thread list in the lower left corner lists all active threads, select 线程and click to display information about the thread, including thread name, status and stack trace.

Click 检测死锁to detect deadlock

insert image description here

kind:

Display the class information loaded by the JVM, including the number of loaded classes, the number of unloaded classes, etc.

insert image description here

Red : Total number of loaded classes (including subsequently unloaded classes)
Blue : Number of currently loaded classes

详细信息The section shows Java VMthe total number of classes loaded since startup, the number currently loaded and the number unloaded.

You can query the class loading status in the specified time range by selecting 时间范围-下拉框.

VM summary:

Display the basic information of JVM, such as JVM startup parameters, command line parameters, etc.

insert image description here

Woman:

Displays information about all MBeans registered on the platform.MBeanserver.

insert image description here

The tree structure on the left shows all MBeans, and after selecting an MBean, its attributes, operations, notifications, and other information will be displayed on the right.


Reference blog:

Java performance analysis jconsole detailed explanation: https://jishu.zuankewang.com/24934/
Jconsole use: https://www.cnblogs.com/walkersss/p/17476083.html
jconsole remote connection use: https://blog. csdn.net/interestANd/article/details/127390435
Jconsole use - Muchen: https://www.cnblogs.com/baby123/p/11395760.html
Java heap memory (Heap): https://www.pianshen.com /article/1576134841/

Guess you like

Origin blog.csdn.net/xhmico/article/details/130720808
use
use