JVM Info Example

0

In this example, we will look into how to retrieve few of the JVM diagnostics information.

Runtime system of the JVM

RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();

Memory system of the JVM

MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();

Java Vendor, Version and home

        StringBuilder buffer = new StringBuilder();
        buffer.append(System.getProperty("java.vendor"));
        buffer.append(" ");
        buffer.append(System.getProperty("java.version"));
        buffer.append(" ");
        buffer.append(System.getProperty("java.home"));
        System.out.println("Java Runtime: " + buffer.toString());

You can also get the version and vendor using the runtimeMXBean.

runtimeMXBean.getVmVersion();
runtimeMXBean.getVmVendor();

PID

<process id>@<host>

System.out.println(runtimeMXBean.getName());

Output:

[email protected]

VM Name

runtimeMXBean.getVmName()

Max Heap Size

Returns the current memory usage of the heap that is used for object allocation. The maximum heap size is defined by the –Xmx option.

memoryMXBean.getHeapMemoryUsage()

For non-heap memory used by the Java virtual machine:

memoryMXBean.getNonHeapMemoryUsage()

The maximum amount of memory in bytes that can be used for memory management.

Initial Heap Size

It represents the initial amount of memory (in bytes) that the Java virtual machine requests from the operating system for memory management during startup. The initial heap size is defined by the –Xms option.

memoryMXBean.getHeapMemoryUsage().getInit()

Used Heap Size

The amount of used memory in the returned memory usage is the amount of memory occupied by both live objects and garbage objects that have not been collected, if any.

heapMemory.getUsed()

Maximum direct memory

JVM chooses the size of NIO direct-buffer allocations automatically. This is the default behavior. This behavior can be changed by explicitly setting the maximum total size of the NIO direct-buffer allocations using -XX:MaxDirectMemorySize. Below code retrieves the value set.

        long directMemoryMax = 0;
        try {
            Class<?> vmClass = Class.forName("sun.misc.VM");
            directMemoryMax = (Long) vmClass.getMethod("maxDirectMemory").invoke(null);
        } catch (Exception t) {
            // ignore
        }

Maximum memory

Runtime.getRuntime().maxMemory();

Free memory

Runtime.getRuntime().freeMemory();

Total memory

Runtime.getRuntime().totalMemory();

JVM Arguments

The input arguments passed to the Java virtual machine which does not include the arguments to the main method. It can include ‘java’ command-line options, environment variables, configuration files, etc.

        List<?> jvmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
        StringBuilder buffer = new StringBuilder();
        for (Object arg : jvmArgs) {
            buffer.append(" ").append(arg);
        }
        System.out.println("    JVM args:" + buffer.toString());

Classpath

String classPath = runtimeMXBean.getClassPath();

System properties

Map<String, String> systemProperties = Collections.unmodifiableMap(runtimeMXBean.getSystemProperties());

Available processors

Runtime runtime = Runtime.getRuntime();      
runtime.availableProcessors();

Start time

The start time of the Java virtual machine in milliseconds.

runtimeMxBean.getStartTime();

Up-time

Time since the JVM started.

runtimeMxBean.getStartTime();

JVM diagnostic example

Here is the example class that retrieves all the above attributes.

JvmDetailsExample:

package com.javarticles.diagnostics;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.RuntimeMXBean;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JvmDetailsExample {
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("java.vendor", System.getProperty("java.vendor"));
        map.put("java.version", System.getProperty("java.version"));
        map.put("java.home", System.getProperty("java.home"));
                
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();

        //split <process id>@<host>
        long pid;
        String xPid = runtimeMXBean.getName();
        System.out.println("xpid: " + xPid);
        try {
            xPid = xPid.split("@")[0];
            pid = Long.parseLong(xPid);
        } catch (Exception e) {
            pid = -1;
        }
        map.put("pid", pid);
        long heapInit = memoryMXBean.getHeapMemoryUsage().getInit() < 0 ? 0
                : memoryMXBean.getHeapMemoryUsage().getInit();
        long heapMax = memoryMXBean.getHeapMemoryUsage().getMax() < 0 ? 0
                : memoryMXBean.getHeapMemoryUsage().getMax();
        long nonHeapInit = memoryMXBean.getNonHeapMemoryUsage().getInit() < 0
                ? 0
                : memoryMXBean.getNonHeapMemoryUsage().getInit();
        long nonHeapMax = memoryMXBean.getNonHeapMemoryUsage().getMax() < 0 ? 0
                : memoryMXBean.getNonHeapMemoryUsage().getMax();
        
        long directMemoryMax = 0;
        try {
            Class<?> vmClass = Class.forName("sun.misc.VM");
            directMemoryMax = (Long) vmClass.getMethod("maxDirectMemory")
                    .invoke(null);
        } catch (Exception t) {
            // ignore
        }
        map.put("maxDirectMemory", directMemoryMax);
        List<?> jvmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
        StringBuilder buffer = new StringBuilder();
        for (Object arg : jvmArgs) {
            buffer.append(" ").append(arg);
        }
        map.put("JVM args", buffer.toString());

        String classPath = runtimeMXBean.getClassPath();
        map.put("classPath", classPath);
        Map<String, String> systemProperties = Collections
                .unmodifiableMap(runtimeMXBean.getSystemProperties());
        map.putAll(systemProperties);
     
        Runtime runtime = Runtime.getRuntime();        
        map.put("runtime.availableProcessors", Integer.valueOf(runtime.availableProcessors()).longValue());
        map.forEach((key, value)->{System.out.println(key + "->" + value);});
    }
}

Download the source code

This example is about JVM diagnostics.

You can download the example here: jvmInfoExample.zip
Share.

Comments are closed.