How to Monitor CPU Memory and Disk Usage in Java

Kibo Hutchinson Developer Tips, Tricks & Resources

In this post, we will discuss some of the primary commands, tools, and techniques that could help to monitor CPU Memory and Disk Usage in Java. 

The Java tools  observe Java bytecode constructs and processes. Java Profilers follow all system commands and processor usage.  This lets you look at call arrangement at whatever point you prefer.

The conventional profiler  provides  a lot of knowledge, the value of which depends on the debugging assignment. Some of the tools are best for aggregating logs across all applications and servers, such as Stackify Retrace, configuring and monitoring automated log queries. Stackify’s Application Performance Management tool, Retrace, offers Java users greater application insights with integrated logging and code profiling.  With integrated centralized and structured logging, access all of your application logs from a single place across all applications and servers.  Start your free, two week trial of Retrace today.  

Here we have listed down the techniques extensively to demo memory and CPU monitoring in Java to improve code quality.

New call-to-action

5 Techniques for Observing CPU Memory and Disk Usage in Java!

1. Basic Linux Commands to Monitor Memory and CPU Usage

a) free –m 

The command renders the available and used memory data of your VM/node.

b) top

This command is for CPU and memory usage.

(Image Source:

When you will hit “1” on the Keyboard, then the top is running and will display all the possible CPUs and the use of each CPU on the screen.

(Image Source:

There are a total of four CPUs —Cpu0, Cpu1, Cpu2, Cpu3— and all of their utilization statistics.

c) meminfo and cpuinfo.

Use the following commands for detailed information regarding VM/node’s memory and processors. 

$cat /proc/cpuinfo

$cat /proc/meminfo

d) CPU Memory and Disk usage of a specific process.

$ps -p [pid] -o %cpu,%mem,cmd

This command is for CPU and memory usage of a Java application method. It also displays which command has initiated this process.

e) It’s thread statistics of the Java method.

$ps uH –p [pid]

This command provides the number of threads in a process and their statistics.

(Image Source:

It shows how much memory and CPU is used per threat and how long they’ve been operating.

When an application is unresponsive, these commands are beneficial to conduct preceding inquiries before leaping into memory and thread holes. These are the essential commands for introductory research aside from memory and CPU statistics.

df –k  –> to check disk space

lsof -i :<port> –> To check, whether a particular port is open

lsof –i: tcp –> All the ESTABLISHED and LISTENING TCP connections.

netstat -plunt  –> Network connectivity

2. Monitoring Memory Usage

Next, let’s look at the ManagementFactory class to monitor memory usage.  This is done by requesting the MemoryMXBean.  The heap memory is essential for applying 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));

This example returns the initial, used, max and committed memory each. Here  is a brief description of each:

  1. Initial: Initial concept of the Java applications from the OS throughout the startup
  2. Used: The memory utilised by the Java
  3. Max: The most memory possible to the JVM. OutOfMemoryException occurs if the memory is maxed out.
  4. Committed: The volume of memory free to the JVM

3. Native Profiling Agent (-Agentpath)

Several java profiling tools are built utilising native libraries for Windows, Linux, or macOS. –Agentpath is the VM used to configure the instrument library.

For instance:

 java –agentpath:/usr/profiler/lib/[agent].so  -jar application.jar

After the profiler’s native instrument library is designed in the remote server, its user interface is combined with the remote server for profiling.

Tools such as JProfiler and Yourkit leverage the native profiling tools.

4. Java Profiling Agent (-Javaagent)

Java profiling agents are instrumented using  the –javaagent VM alternative. You can create a lightweight agent in Java with these steps.

  • The instrumentation agent needs to develop a premain() system
  • The agent class must be designed as a Pre-Main position in the MANIFEST.MF record of the JAR data, where the agent and its subordinate classes are created.
  • The JVM starts with –javaagen.

java -javaagent:agent.jar -jar application.jar

The agent class premain() process is before the main() process of the utilisation, -javaagent should introduce -jar.

If the system instrumentation expects the agent to be placed after the application’s main() system, the next step is to have an agentmain() method in a different Agent Class.  It should be  constructed as Agent-Main class in the MANIFEST.MF record

5. Agent Library (-Agentlib)

-Agentlib is a VM used to arrange the local library throughout the JVM project. The main purpose to use it for the two reasons:

1) Loading HPROF 

2) To control the JVM in debugger form

1) Using HPROF:

HPROF profiles the Heap and CPU that are transmitted adjacent to Java. It creates the language efficiently. –agentlib:hprof and –Xrunhprof are generally used as VM parameters with HPROF. 

The JVM tracks data regarding heap profiles, designated concepts, stack traces within the java.hprof.txt.  

java agentlib:hprof=heap=sites

HPROF can be covered during collection (javac) so the developer can pinpoint bottlenecks. 

javac -J-agentlib:hprof=heap=sites

2) Running an Application in Debug Mode

Running an application in debug form is approved by utilising the –agentlib:jdwp VM part. This decision places the JVM’s in-process debugging archives.

 java -agentlib:jdwp=transport=dt_socket,address=,

server=y,suspend=y –jar application.jar 

This parameter supports debugging an app running utilising an IDE.

Simple Steps

Use the restrictedly/remotely command for the debugger method in Java. The application monitoring records in the designated port will not move further until the step (ii) is finished.

Snap from the application record: Monitoring for transport dt_socket at address: 9001

Initiate the project in IDE in debug form and join the cypher to the Host and Port.  

Debug the code on the exclusive server with the IDE.