1. Overview
1.概述
In this quick tutorial, we’ll be discussing how to monitor key metrics in Java. We’ll focus on disk space, memory usage, and thread data – using only core Java APIs.
在这个快速教程中,我们将讨论如何在Java中监控关键指标。我们将重点讨论磁盘空间、内存使用和线程数据–仅使用核心Java APIs。
In our first example, we’ll make use of the File class to query specific disk information.
在我们的第一个例子中,我们将利用File类来查询特定的磁盘信息。
Then, we’ll analyze memory usage and processor information by diving into the ManagementFactory class.
然后,我们将通过深入研究ManagementFactory类来分析内存使用和处理器信息。
Finally, we’ll touch on how to monitor these key metrics at runtime using Java Profilers.
最后,我们将谈谈如何在运行时使用Java Profilers监控这些关键指标。
2. Introduction to the File Class
2.文件类介绍
Simply put, the File class represents an abstraction of a file or directory. It can be used to obtain key information about the file system and maintain OS independence regarding file paths. In this tutorial, we’ll be using this class to examine root partitions on both Windows and Linux machines.
简单地说,文件类代表了一个文件或目录的抽象概念。它可用于获取文件系统的关键信息,并保持关于文件路径的操作系统独立性。在本教程中,我们将使用这个类来检查Windows和Linux机器上的根分区。
3. ManagementFactory
3.管理工厂
Java provides the ManagementFactory class as a factory for getting managed beans (MXBeans) containing specific information about the JVM. We’ll be examining two in the following code examples:
Java提供了ManagementFactory 类,作为获取管理Bean的工厂(MXBeans)包含关于JVM的特定信息。我们将在下面的代码示例中研究两个。
3.1. MemoryMXBean
3.1 MemoryMXBean
The MemoryMXBean represents the management interface for the memory system of the JVM. On runtime, the JVM creates a single instance of this interface which we can retrieve using the ManagementFactory‘s getMemoryMXBean() method.
MemoryMXBean代表JVM内存系统的管理接口。在运行时,JVM创建了这个接口的一个实例,我们可以使用ManagementFactory的getMemoryMXBean()方法来检索。
3.2. ThreadMXBean
3.2.ThreadMXBean
Similarly to MemoryMXBean, ThreadMXBean is the management interface for the thread system of the JVM. It can be called using the getThreadMXBean() method and holds key data regarding threads.
与MemoryMXBean类似,ThreadMXBean是JVM线程系统的管理接口。它可以通过getThreadMXBean()方法被调用,并持有关于线程的关键数据。
In the following examples, we’ll be using the ThreadMXBean to get our hands on the JVM’s ThreadInfo class – which contains specific information regarding threads running on the JVM.
在下面的例子中,我们将使用ThreadMXBean来获取JVM的ThreadInfo类–它包含关于在JVM上运行的线程的具体信息。
3. Monitoring Disk Usage
3.监测磁盘使用情况
In this code example, we’ll use the File class to contain key information about partitions. The following example will return the free, total and available space from the C: drive on a Windows machine:
在这个代码例子中,我们将使用文件类来包含关于分区的关键信息。下面的例子将返回Windows机器上C: 驱动器的自由空间、总空间和可用空间。
File cDrive = new File("C:");
System.out.println(String.format("Total space: %.2f GB",
(double)cDrive.getTotalSpace() /1073741824));
System.out.println(String.format("Free space: %.2f GB",
(double)cDrive.getFreeSpace() /1073741824));
System.out.println(String.format("Usable space: %.2f GB",
(double)cDrive.getUsableSpace() /1073741824));
Similarly, we can return the same information for the root directory of a Linux machine:
同样,我们可以为Linux机器的根目录返回同样的信息。
File root = new File("/");
System.out.println(String.format("Total space: %.2f GB",
(double)root.getTotalSpace() /1073741824));
System.out.println(String.format("Free space: %.2f GB",
(double)root.getFreeSpace() /1073741824));
System.out.println(String.format("Usable space: %.2f GB",
(double)root.getUsableSpace() /1073741824));
The above code prints out the total, free and usable space for the defined file. By default, the above methods provide the number of bytes. We’ve converted these bytes into gigabytes to make the result much more human readable.
上述代码打印出定义文件的总空间、自由空间和可用空间。默认情况下,上述方法提供的是字节数。我们把这些字节转换成千兆字节,以使结果更容易被人阅读。
4. Monitoring Memory Usage
4.监控内存使用情况
We’ll now use the ManagementFactory class to query the memory available to the JVM by calling the MemoryMXBean.
现在我们将使用ManagementFactory类来通过调用MemoryMXBean查询JVM的可用内存。
In this example, we’ll focus primarily on querying heap memory. It is important to note that non-heap memory can also be queried using MemoryMXBean:
在这个例子中,我们将主要关注于查询堆内存。需要注意的是,非堆内存也可以使用MemoryMXBean查询:。
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
System.out.println(String.format("Initial memory: %.2f GB",
(double)memoryMXBean.getHeapMemoryUsage().getInit() /1073741824));
System.out.println(String.format("Used heap memory: %.2f GB",
(double)memoryMXBean.getHeapMemoryUsage().getUsed() /1073741824));
System.out.println(String.format("Max heap memory: %.2f GB",
(double)memoryMXBean.getHeapMemoryUsage().getMax() /1073741824));
System.out.println(String.format("Committed memory: %.2f GB",
(double)memoryMXBean.getHeapMemoryUsage().getCommitted() /1073741824));
The above example returns the initial, used, max and committed memory respectively. Here’s a short explanation of what that means:
上面的例子分别返回了初始内存、已用内存、最大内存和承诺内存。下面是对该含义的简短解释。
- Initial: Initial memory the JVM requests from the OS during startup
- Used: The current amount of memory used by the JVM
- Max: The maximum memory available to the JVM. If this limit is reached an OutOfMemoryException may be thrown
- Committed: The amount of memory guaranteed to be available to the JVM
5. CPU Usage
5.CPU的使用情况
Next, we’ll use the ThreadMXBean to gain a comprehensive list of ThreadInfo objects and query them to gain useful information regarding the current threads running on the JVM.
接下来,我们将使用ThreadMXBean来获得ThreadInfo对象的综合列表,并查询它们以获得关于在JVM上运行的当前线程的有用信息。
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
for(Long threadID : threadMXBean.getAllThreadIds()) {
ThreadInfo info = threadMXBean.getThreadInfo(threadID);
System.out.println("Thread name: " + info.getThreadName());
System.out.println("Thread State: " + info.getThreadState());
System.out.println(String.format("CPU time: %s ns",
threadMXBean.getThreadCpuTime(threadID)));
}
Firstly, the code gets a list of current threads using the getAllThreadIds method. For each thread, it then outputs the name and state of the thread followed by the CPU time for the thread in nanoseconds.
首先,代码使用getAllThreadIds方法获得一个当前线程的列表。对于每一个线程,它都会输出线程的名称和状态,然后是该线程的CPU时间(纳秒)。
6. Monitoring Metrics Using Profilers
6.使用分析器监测指标
Finally, it’s worth mentioning that we can monitor these key metrics without using any Java code. Java Profilers closely monitor key constructs and operations at the JVM level and offer real-time analysis of memory, threads and more.
最后,值得一提的是,我们可以不使用任何Java代码来监控这些关键指标。Java Profilers密切监测JVM层面的关键结构和操作,并提供内存、线程等方面的实时分析。
VisualVM is one such example of a Java profiler and has been bundled with the JDK since Java 6. Many integrated development environments (IDE)s contain plugins to leverage profilers whilst developing new code. You can learn more about Java Profilers and VisualVM here.
VisualVM就是这样一个Java剖析器的例子,从Java 6开始就与JDK捆绑在一起。许多集成开发环境(IDE)包含插件,以便在开发新代码时利用剖析器。您可以在这里了解有关Java剖析器和VisualVM的更多信息。
7. Conclusion
7.结语
In this article, we’ve touched on using core Java APIs to query key information about disk usage, memory management, and thread information.
在这篇文章中,我们已经谈到了使用核心Java APIs来查询磁盘使用、内存管理和线程信息等关键信息。
We’ve looked at multiple examples of using the File and ManagmentFactory classes to obtain these metrics.
我们已经看了多个使用文件和管理工厂类来获得这些指标的例子。