Retrace will reach End of Life on March 31, 2027. Click here to learn more.

How to Monitor CPU Memory and Disk Usage in Java

  |  May 24, 2021
How to Monitor CPU Memory and Disk Usage in Java

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.

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: https://dzone.com/articles/java-memory-and-cpu-monitoring-tools-and-technique)

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: https://dzone.com/articles/java-memory-and-cpu-monitoring-tools-and-technique)

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: https://dzone.com/articles/java-memory-and-cpu-monitoring-tools-and-technique)

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 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.

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

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.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More
situs toto pucuk4d indobet toto slot slot depo 10k 8kuda4d toto toto toto bobatoto daftar logototo situs toto situs toto toto patentoto situs toto slot 4D leon188 situs toto toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 lingkartoto https://foundvinylrecords.com/ depo 5k panen100 semibola popo togel https://asupantoto.co/ slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot situs toto slot situs slot link slot popotogel login nobu99 logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo bobatoto istanaslot rp888 www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d toto slot slot gacor slot bento11 link slot gacor KARI4D PUCUK4D pascol4d situs slot toto slot titi4d slot gacor toto toto slot slot toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot AMANAHTOTO https://www.asupan188.dev/ slot gacor gampang menang coloktoto toto slot toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor situs slot link slot toto toto toto slot gacor slot resmi slot88 situs toto MySlot188 SlotPoker188 slot gacor slot gacor pucuk4d PASCOL4D slot gacor slot gacor situs slot link slot pentaslot SLOT GACOR AMANAHTOTO TOTO 4D pajaktoto slotvip toto togel situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 toto slot slot gacor situs slot link slot slot thailand patentoto situs judi bola situs toto roma77 slot jpmania toto228 RATU89 RUSIA777 slot gacor POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat PARLAY slot gacor slot gacor slot gacor togel link slot BUNGASLOT slot slot gacor situs slot slot gacor slot gacor sejukbos link slot gacor situs slot link slot slot gacor slot gacor StreamEast
situs toto pucuk4d indobet toto slot slot depo 10k 8kuda4d toto toto toto bobatoto daftar logototo situs toto situs toto toto patentoto situs toto slot 4D leon188 situs toto toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 lingkartoto https://foundvinylrecords.com/ depo 5k panen100 semibola popo togel https://asupantoto.co/ slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot situs toto slot situs slot link slot popotogel login nobu99 logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo bobatoto istanaslot rp888 www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d toto slot slot gacor slot bento11 link slot gacor KARI4D PUCUK4D pascol4d situs slot toto slot titi4d slot gacor toto toto slot slot toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot AMANAHTOTO https://www.asupan188.dev/ slot gacor gampang menang coloktoto toto slot toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor situs slot link slot toto toto toto slot gacor slot resmi slot88 situs toto MySlot188 SlotPoker188 slot gacor slot gacor pucuk4d PASCOL4D slot gacor slot gacor situs slot link slot pentaslot SLOT GACOR AMANAHTOTO TOTO 4D pajaktoto slotvip toto togel situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 toto slot slot gacor situs slot link slot slot thailand patentoto situs judi bola situs toto roma77 slot jpmania toto228 RATU89 RUSIA777 slot gacor POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat PARLAY slot gacor slot gacor slot gacor togel link slot BUNGASLOT slot slot gacor situs slot slot gacor slot gacor sejukbos link slot gacor situs slot link slot slot gacor slot gacor StreamEast