How to Monitor Disk, Memory, and Threads in Java?

0 102

Get real time updates directly on you device, subscribe now.

4 min read

Monitoring Disk, Memory, and Threads in Java

In this journal entry, we’ll be showcasing how to monitor Disk, Memory, and Threads using Core Java.

For the Disk usage, we will make use of the File class of Java for querying the specific disk information.

After the Disk usage, we’ll look into the ManagementFactory class for querying the information about the JVM (Java Virtual Machine).

Introduction to the File class – Disk Usage Monitoring

In a very simple word, the File class represents an abstraction of a file or directory. We can use this class to get some of the key information about the file system. We will be using this class to examine the root partitions on both Windows and Linux based systems.

To get the file systems information on Windows:

File rootDrive = new File("C:");
System.out.println(String.format("Total space: %.2f GB",
  (double)rootDrive.getTotalSpace() /1073741824));
System.out.println(String.format("Free space: %.2f GB", 
  (double)rootDrive.getFreeSpace() /1073741824));
System.out.println(String.format("Usable space: %.2f GB", 
  (double)rootDrive.getUsableSpace() /1073741824));

To get the file systems information on Linux:

File rootDrive = new File("/");
System.out.println(String.format("Total space: %.2f GB", 
  (double)rootDrive.getTotalSpace() /1073741824));
System.out.println(String.format("Free space: %.2f GB", 
  (double)rootDrive.getFreeSpace() /1073741824));
System.out.println(String.format("Usable space: %.2f GB", 
  (double)rootDrive.getUsableSpace() /1073741824));

This piece of code prints out the total, free, and usable space. By default, the above-mentioned methods like getTotalSpace, getFreeSpace, getUsableSpace all provide the data in the number of bytes. To make these numbers more human-friendly while reading we have converted them to GB (gigabytes).

Introduction to the ManagementFactory class – Memory and Threads Usage Monitoring

ManagementFactory is provided in Java as a factory class for getting managed beans MXBeans. This class consists of static methods each of which contains the specific information about the JVM running on the machine. For this journal article, we will be focusing on two bean classes, MemoryMXBean and ThreadMXBean.

MemoryMXBean: The MemoryMXBean represents the management interface for the memory system of the JVM. A Java virtual machine has a single instance of the implementation class of this interface which we can retrieve using the ManagementFactory.getMemoryMXBean() method. In this example, our focus is primarily on the heap memory. Although, we can also query the non-heap memory using the 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("Committed memory: %.2f GB", 
  (double)memoryMXBean.getHeapMemoryUsage().getCommitted() /1073741824));

The above example returns the following types of memory information:

Initial: Initial memory the JVM requests from the OS during startup
Used: The current amount of memory used by the JVM
Committed: The amount of memory guaranteed to be available to the JVM

ThreadMXBean: Similarly to MemoryMXBean, ThreadMXBean is the management interface for the thread system of the JVM. It can be called using the ManagementFactory.getThreadMXBean() method. This method holds the key data regarding threads. In the below-mentioned example, we’ll be having a look at the ThreadMXBean and the JVM’s ThreadInfo class. This is the main class which contains specific information regarding the currently running threads on the 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)));
  }

Let’s understand what this piece of code is doing in little details. Firstly, the code is getting a list of threads using the getAllThreadIds method. And then for each thread, we are printing the name and state of the thread followed by the CPU time. The time here is in nanoseconds.

Conclusion

In this journal entry, we have used the core Java APIs to get key information regarding the disk usage, memory usage, and also retrieved the thread information. However, if you are a little lazy writing code for yourself, then to help you guys, there are Profilers available in the Java market for monitoring these metrics. VisualVM is one of the examples of a Java Profiler and it has been bundled with the JDK since Java 6.