Article directory
1. Introduction
JConsole
is a Java
monitoring and management console tool that can be used to monitor Java
the performance and resource utilization of virtual machines (JVMs). It provides a graphical interface to view real-time JVM
running status, memory usage, thread activity, garbage collection, and other information, as well as perform some management operations.
Using JConsole
can help developers and system administrators 性能调优、内存泄漏排查和故障诊断
to improve application reliability and performance.
In short, JConsole
it is a powerful Java
monitoring and management tool that can help developers and system administrators monitor and manage Java
application performance and resource utilization in real time.
Two, use
If you don't have JDK installed, please refer to the blog:
- windows: java-environment configuration (detailed tutorial)
- linux: Linux-java environment configuration
Here are JConsole
the steps to use:
-
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.exe
to start JConsole. -
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. -
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 /bin
the directory to find it, jconsole.exe
double-click to run it
If you configure the Java environment correctly, win + R
open 命令提示符
the window and enter jconsole
the command can also be opened
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
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不安全的连接
You can enter the monitoring interface
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.password
a file for 控制远程连接用户名和密码
.
You can %JAVA_HOME%/jre/lib/management
check whether there is jmxremote.password
a file in the directory, if not, you can use jmxremote.password.template
the file in the directory as a prototype to copy a
cp jmxremote.password.template jmxremote.password
then edit jmxremote.password
the file
vim jmxremote.password
Modify the bottom two lines and #
remove the comment
# monitorRole QED
# controlRole R&D
monitorRole
It is the viewing role (also the login user name, in access
the file), has read-only permission, QED
and is the set password.
controlRole
Is the control user (also the login user name, in access
the file), has read and write permissions, R&D
and is the set password.
Finally, authorize the file:
chmod 600 jmxremote.password
connect:
The connection is successful as shown in the figure:
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.
Memory:
Displays the memory usage of the JVM, including heap memory, non-heap memory, memory pool, etc.
内存
The interface has 执行GC
a 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 VM
Manages two kinds of memory: heap memory and non-heap memory, both of which are Java VM
created 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:
-
Heap memory can be divided into two main areas:
- Young/New Generation: Newborn objects are placed in the Young/New Generation, which consists of
Eden
andSurvivor Space
.Eden
The ratio ofSurvivor Space
the space size to is by default8:1
, that is, whenYoung/New Generation
the total space size of the area is10M
, the space size of Eden is8M
,Survivor Space
and the two blocks are allocated respectively1M
. This ratio can be-XX:SurvivorRatio
modified by the parameter.Young/New Generation
The size of can-Xmn
be 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
Survivor
area. - Survivor Space : The survivor area is used to save objects that have not been recycled after garbage collection in the Eden memory area.
Survivor Space
Divided into two areas of the same size, namelys0
ands1
, when triggered,Minor GC
move the surviving objects tos0
(From Eden To s0), so that Eden is cleared and can be allocated to new objects; when triggeredMinor GC
again ,s0
andEden
the surviving objects in and are moved tos1
(From s0 To s1),s0
that is, they are emptied. At the same time, onlyEden
and oneSurvivor Space
are operated at the same time, so at least one of the two areas ofs0
and will be free at the same time.s1
Survivor
- 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
- 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
-Xmx
and-Xmn
the two parameters.
- Young/New Generation: Newborn objects are placed in the Young/New Generation, which consists of
-
By default, the ratio of the young generation (Young) to the old generation (Old) is
1:2
(this value can be-XX:NewRato
specified 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-Xmx
maximum The limit canMinHeapFreeRatio
be adjusted through the parameter; when the default free heap memory is greater than 70%, the JVM will reduce the heap to the-Xms
minimum limit, which canMaxHeapFreeRatio
be adjusted through the parameter.
-
-
(2) Non-heap memory: includes
Java VM
the 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 VM
it 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 jvm
the 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 VM
Also includes Code Cache, which contains memory used to compile and store native code. - Compressed Class Space: compressed class space
- Metaspace: Metaspace is
- It can be divided into:
thread:
Displays thread activity in the JVM, including thread count, thread state, deadlocks, and more.
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
kind:
Display the class information loaded by the JVM, including the number of loaded classes, the number of unloaded classes, etc.
Red : Total number of loaded classes (including subsequently unloaded classes)
Blue : Number of currently loaded classes
详细信息
The section shows Java VM
the 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.
Woman:
Displays information about all MBeans registered on the platform.MBeanserver.
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/