Skip to main content

Lesson 6 of 8

JVM Monitoring & Profiling Tools

You can't optimise what you can't measure. Java provides built-in monitoring tools and supports industry-standard profilers. Knowing which tool to use for which problem (memory leaks, CPU bottlenecks, GC pressure, thread contention) is a critical production skill.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

JVM Monitoring Tools

JVM Monitoring Tools
// Built-in JDK Tools:
// jps     - list Java processes: jps -v
// jstack  - thread dump: jstack <pid> > thread_dump.txt (diagnose deadlocks, high CPU)
// jmap    - heap dump: jmap -dump:live,format=b,file=heap.hprof <pid>
// jstat   - GC stats live: jstat -gcutil <pid> 1000  (every 1 second)
// jcmd    - comprehensive: jcmd <pid> VM.flags | GC.heap_info | Thread.print
// jinfo   - read/change JVM flags: jinfo -flag +HeapDumpOnOutOfMemoryError <pid>

// Programmatic monitoring (MXBeans):
import java.lang.management.*;
import java.util.List;

public class JVMMonitor {
    public static void printJVMInfo() {
        // Memory
        MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
        System.out.printf("Heap used:    %.1f MB%n",
            mem.getHeapMemoryUsage().getUsed() / 1e6);
        System.out.printf("Heap max:     %.1f MB%n",
            mem.getHeapMemoryUsage().getMax() / 1e6);
        System.out.printf("Non-heap:     %.1f MB%n",
            mem.getNonHeapMemoryUsage().getUsed() / 1e6);

        // GC
        for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
            System.out.printf("GC %-25s count=%-5d time=%d ms%n",
                gc.getName(), gc.getCollectionCount(), gc.getCollectionTime());
        }

        // Threads
        ThreadMXBean threads = ManagementFactory.getThreadMXBean();
        System.out.printf("Threads live: %d (peak: %d)%n",
            threads.getThreadCount(), threads.getPeakThreadCount());
        long[] deadlocked = threads.findDeadlockedThreads();
        if (deadlocked != null) System.out.println("DEADLOCK DETECTED!");

        // CPU
        OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
        System.out.printf("CPU load: %.1f%%%n", os.getSystemLoadAverage() * 100);
        System.out.printf("Processors: %d%n", os.getAvailableProcessors());
    }

    // JFR (Java Flight Recorder - Java 11+ open source)
    // jcmd <pid> JFR.start name=recording duration=30s filename=recording.jfr
    // jcmd <pid> JFR.dump filename=instant.jfr
    // Open in JDK Mission Control (JMC) for visual analysis

    // VisualVM - free GUI tool (download from visualvm.github.io)
    // Attach to running process, monitor heap, GC, threads, CPU samples
}

Quick Quiz

Tip

Tip

Practice JVM Monitoring Profiling Tools in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for JVM Monitoring & Profiling Tools

Practice Task

Note

Practice Task - (1) Write a working example of JVM Monitoring Profiling Tools from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with JVM Monitoring Profiling Tools is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Topics in This Module

0/8 done