用 jconsole 在 Linux 上监控你的 Java
Java

> 
> 如何使用 Java 开发工具包中的 Java 监控和管理控制台。
> 
> 
> 


![](/data/attachment/album/202110/08/131129bf3h3gv18388fl11.jpg)


Java 开发工具包（JDK）提供了开发 Java 应用程序的二进制文件、工具和编译器。其中一个有用的工具是 `jconsole`。


为了演示，我将使用 WildFly J2EE 应用服务器，它是 JBOSS 开源应用服务器项目的一部分。首先，我启动了一个独立的实例。



```
~/wildfly/24.0.1/bin$ ./standalone.sh
=========================================================================
  JBoss Bootstrap Environment
  JBOSS_HOME: /home/alan/wildfly/24.0.1
  JAVA: /usr/lib/jvm/java-11-openjdk-11.0.11.0.9-5.fc34.x86_64/bin/java

```

现在，在另一个终端，输入 `jconsole`。



```
$ jconsole

```

启动后，jconsole 会列出本地实例。选择“<ruby> 本地进程 <rt>  Local Process </rt></ruby>”，然后选择进程的名称并点击“<ruby> 连接 <rt>  Connect </rt></ruby>”。这就是连接并开始使用运行中的 Java 虚拟机（JVM）的 jconsole 的全部过程。


![jconsole 与本地进程的新连接屏幕](/data/attachment/album/202110/08/131136m8wqj69qkwk90rzj.png "new connection")


### 概述


这个 Java 监控和管理控制台在仪表板的顶部显示进程标识符（PID）。“<ruby> 概述 <rt>  Overview </rt></ruby>”标签有四个图表，显示“<ruby> 堆内存使用情况 <rt>  Heap Memory Usage </rt></ruby>”、“<ruby> 线程 <rt>  Threads </rt></ruby>”、“<ruby> 类 <rt>  Classes </rt></ruby>”和“<ruby> CPU 使用情况 <rt>  CPU Usage </rt></ruby>”的重要信息。


![jconsole 仪表板显示堆内存使用量、线程、类和 CPU 使用量](/data/attachment/album/202110/08/131137qk2lkqddkvdlv8r8.png "tab overview")


沿着顶部的标签提供每个区域的更详细的视图。


### 内存


“<ruby> 内存 <rt>  Memory </rt></ruby>”标签显示 JVM 所使用的内存的各个方面的图表。分配给 JVM 的服务器系统内存量被称为“<ruby> 堆 <rt>  Heap </rt></ruby>”。这个屏幕还提供了关于堆的内部组件使用情况的详细信息，例如 “<ruby> 伊甸园 <rt>  Eden Space </rt></ruby>”、“<ruby> 老年代 <rt>  Old Gen </rt></ruby>” 和 “<ruby> 幸存者区 <rt>  Survivor Space </rt></ruby>”。你也可以手动请求一个垃圾收集动作。


![jconsole 内存标签](/data/attachment/album/202110/08/131137tzd4ynzxg39xgggn.png "memory tab")


### 线程


“<ruby> 线程 <rt>  Threads </rt></ruby>”标签显示有多少线程在运行。你也可以手动检查是否存在死锁。


![jconsole 线程仪表板显示了随时间变化的线程数量和滚动的线程列表](/data/attachment/album/202110/08/131137mw40o0jozz4meomo.png "threads tab")


### 类


“<ruby> 类 <rt>  Classes </rt></ruby>”标签告诉你有多少类被加载，有多少被卸载。


![jconsole 类标签显示随着时间推移加载的类数量](/data/attachment/album/202110/08/131138i00nh2mbuyaghuze.png "classes tab")


### 虚拟机摘要


“<ruby> 虚拟机摘要 <rt>  VM Summary </rt></ruby>”标签提供了许多关于应用程序和主机系统的细节。你可以了解你所处的操作系统和架构、系统内存总量、CPU 数量，甚至交换空间。


![jconsole 虚拟机摘要标签](/data/attachment/album/202110/08/131138q3slr1secu5slbjt.png "VMsummary tab ")


摘要中显示的关于 JVM 的进一步细节，包括当前和最大的堆大小以及正在使用的垃圾收集器的信息。底部的窗格列出了传递给 JVM 的所有参数。


### MBeans


最后一个标签，MBeans，让你通过所有的 MBeans 向下钻取，以查看每个 MBeans 的属性和值。


![MBeans 标签](/data/attachment/album/202110/08/131138qwtfriqlutiitn5u.png "MBeans tab")


### 总结


Java 已经存在了很长时间，它将继续为全球数百万的系统提供动力。有很多开发环境和监控系统可以使用，但像 `jconsole` 这样的包含在基本工具包中的工具非常有价值。




---


via: <https://opensource.com/article/21/10/monitor-java-linux-jconsole>


作者：[Alan Formy-Duval](https://opensource.com/users/alanfdoss) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
The Java Development Kit (JDK) provides binaries, tools, and compilers for the development of Java applications. One helpful tool included is jconsole.

To demonstrate, I will use the WildFly J2EE application server, which is part of the JBOSS open source application server project. First, I start up a standalone instance.

```
~/wildfly/24.0.1/bin$ ./standalone.sh
=========================================================================
JBoss Bootstrap Environment
JBOSS_HOME: /home/alan/wildfly/24.0.1
JAVA: /usr/lib/jvm/java-11-openjdk-11.0.11.0.9-5.fc34.x86_64/bin/java
```


Now, in another terminal, I type `jconsole`

.

$ jconsole

Upon launching, jconsole lists local instances. Select Local Process, then select the name of the process and click Connect. That is all it takes to connect and begin using jconsole with a running Java Virtual Machine (JVM).

![new connection jconsole new connection screen with local processes](https://opensource.com/sites/default/files/uploads/jconsole_new_connection_local.png)

(Alan Formy-Duvall, CC BY-SA 4.0)

## Overview

The Java Monitoring and Management Console shows the process identifier (PID) at the top of the dashboard. The Overview tab has four graphs to show the vitals for Heap Memory Usage, Threads, Classes, and CPU Usage.

![tab overview jconsole dashboard showing heap memory usage, threads, classes, and CPU usage](https://opensource.com/sites/default/files/uploads/jconsole_tab_overview.png)

(Alan Formy-Duvall, CC BY-SA 4.0)


The tabs along the top provide more detailed views of each area.

## Memory

The Memory tab displays graphs of various aspects of the memory being used by the JVM. The amount of the server system memory allocated to the JVM is called the heap. This screen also provides details about usage by the internal components of the heap, such as the Eden Space, Old Gen, and the Survivor Space. You can manually request a garbage collection action as well.

![jconsole memory tab](https://opensource.com/sites/default/files/uploads/jconsole_tab_memory.png)

(Alan Formy-Duvall, CC BY-SA 4.0)

## Threads

The Threads tab shows how many threads are running. You can also manually check for deadlocks.

![threads tab jconsole thread dashboard showing number of threads over time and a scrolling list of threads](https://opensource.com/sites/default/files/uploads/jconsole_tab_threads.png)

(Alan Formy-Duvall, CC BY-SA 4.0)

## Classes

The classes tab tells you how many classes are loaded and how many have been unloaded.

![classes tab jconsole classes tab showing number of loaded classes over time](https://opensource.com/sites/default/files/uploads/jconsole_tab_classes.png)

(Alan Formy-Duvall, CC BY-SA 4.0)

## VM Summary

The VM Summary tab provides many details about the application and the host system. You can learn which operating system and architecture you are on, the total amount of system memory, the number of CPUs, and even swap space.

![VMsummary tab jconsole VMsummary tab](https://opensource.com/sites/default/files/uploads/jconsole_tab_vm-summary.png)

(Alan Formy-Duvall, CC BY-SA 4.0)

Further details about the JVM shown in the summary include current and maximum heap size and information about the garbage collectors in use. The bottom pane lists all of the arguments passed to the JVM.

## MBeans

The last tab, MBeans, lets you drill down through all of the MBeans to view attributes and values for each.

![MBeans tab MBeans tab](https://opensource.com/sites/default/files/uploads/jconsole_tab_mbeans.png)

(Alan Formy-Duvall, CC BY-SA 4.0)

## Conclusion

Java has been around a long time, and it continues to power millions of systems worldwide. Plenty of development environments and monitoring systems are available, but having a tool like jconsole included in the base kit can be highly valuable.

## Comments are closed.