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.
a) free –m
The command renders the available and used memory data of your VM/node.
This command is for CPU and memory usage.
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.
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.
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.
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
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”,
System.out.println(String.format(“Used heap memory: %.2f GB”,
System.out.println(String.format(“Max heap memory: %.2f GB”,
System.out.println(String.format(“Committed memory: %.2f GB”,
This example returns the initial, used, max and committed memory each. Here is a brief description of each:
Several java profiling tools are built utilising native libraries for Windows, Linux, or macOS. –Agentpath is the VM used to configure the instrument library.
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.
Java profiling agents are instrumented using the –javaagent VM alternative. You can create a lightweight agent in Java with these steps.
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
-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 Hello.java
HPROF can be covered during collection (javac) so the developer can pinpoint bottlenecks.
javac -J-agentlib:hprof=heap=sites Hello.java
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.
server=y,suspend=y –jar application.jar
This parameter supports debugging an app running utilising an IDE.
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.