JVM Configuration: Command-Line Tools and Performance Tuning Parameters

Overview of JVM Parameters

JVM parameters are categorized based on their stability and purpose. Understanding these is essential for fine-tuning application performance and troubleshooting memory issues.

1. Standard Parameters

These are stable options across different versions of the Java Virtual Machine. Common examples include:

  • -version: Displays the installed Java version.
  • -help: Shows help documentation for standard options.
  • -server: Selects the Java HotSpot Server VM.
  • -cp or -classpath: Defines the search path for class files.

2. Non-Standard Parameters (-X)

The -X parameters are specific to the implementation and might change between major releases. They often control execution modes:

  • -Xint: Forces the JVM to run in interpreted-only mode.
  • -Xcomp: Forces compilation of methods on first invocation.
  • -Xmixed: The default mode where the JVM decides whether to interpret or compile code based on execution frequency.

3. Advanced Runtime Parameters (-XX)

These are the most powerful parameters, divided into two types:

  • Boolean Flags: Used to enable or disable features. The syntax is -XX:+<name> (enable) or -XX:-<name> (disable).
    Example: -XX:+UseG1GC enables the Garbage-First garbage collector.
  • Key-Value Flags: Used to set specific numeric or string values. The syntax is -XX:<name>=<value>.
    Example: -XX:MaxGCPauseMillis=200 sets a target for maximum GC pause time.

Memory Allocation Shorthands

Several commonly used heap settings are actually aliases for advanced -XX flags:

  • -Xms is equivalent to -XX:InitialHeapSize
  • -Xmx is equivalent to -XX:MaxHeapSize
  • -Xss is equivalent to -XX:ThreadStackSize

CLI Diagnostics and Monitoring

The JDK provides several commmand-line tools to inspect running Java processes.

jps (Java Process Status)

Lists the instrumented JVMs on the target system.

# List process IDs and main class names
jps -l

jinfo (Configuration Info)

Retrieves configuration information from a running process and allows dynamic adjustment of "manageable" flags.

# View a specific flag
jinfo -flag UseG1GC <PID>

# Update a flag dynamically
jinfo -flag <name>=<value> <PID>

# View all system properties
jinfo -sysprops <PID>

jstat (JVM Statistics)

Used for monitoring resource consumption and performance metrics like GC behavior and class loading.

# Monitor GC statistics every 1s for 5 iterations
jstat -gc <PID> 1000 5

jstack (Thread Stack Trace)

Generates a snapshot of all threads currently running in the JVM, which is vital for detecting deadlocks.

Option Description
-F Force a stack dump if the process is unresponsive.
-l Long listing; includes informasion about ownable synchronizers and locks.

Example: Simultaing a Deadlock

public class DeadlockTest {
    private static final Object LockA = new Object();
    private static final Object LockB = new Object();

    public static void main(String[] args) {
        Thread worker1 = new Thread(() -> {
            synchronized (LockA) {
                System.out.println("Worker 1: Holding Lock A...");
                try { Thread.sleep(50); } catch (Exception e) {}
                synchronized (LockB) {
                    System.out.println("Worker 1: Acquired Lock B");
                }
            }
        });

        Thread worker2 = new Thread(() -> {
            synchronized (LockB) {
                System.out.println("Worker 2: Holding Lock B...");
                try { Thread.sleep(50); } catch (Exception e) {}
                synchronized (LockA) {
                    System.out.println("Worker 2: Acquired Lock A");
                }
            }
        });

        worker1.start();
        worker2.start();
    }
}

jmap (Memory Map)

Generates heap dumps or provides heap summaries.

# Print heap summary
jmap -heap <PID>

# Generate a binary heap dump for analysis
jmap -dump:format=b,file=snapshot.hprof <PID>

Visual Monitoring Tools

JVisualVM and JConsole

Standard GUI tools included with the JDK for monitoring CPU, heap usage, and thread activity. For remote monitoring, JMX must be enabled on the server:

# Example configuration for Tomcat's catalina.sh
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote \
-Djava.rmi.server.hostname=192.168.1.10 \
-Dcom.sun.management.jmxremote.port=9000 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=true"

Alibaba Arthas

A powerful open-source diagnostic tool that allows developers to inspect running code without restarting the application.

  • dashboard: Live metrics of the system.
  • thread: Detailed thread analysis.
  • watch: Inspect method parameters and return values in real-time.
  • trace: Profile method execution time throughout the call stack.

Memory and GC Analysis

Memory Analyzer Tool (MAT)

An Eclipse-based tool designed to analyze .hprof heap dumps. Key concepts include:

  • Shallow Heap: The memory consumed by one object itself.
  • Retained Heap: The total amount of memory that would be freed if this object were garbage collected (includes referenced objects).
  • Leak Suspects Report: Automatically identifies potential memory leaks.

GC Log Analysis

To generate comprehensive GC logs, use the following flags:

-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:/path/to/gc.log

Logs can be analyzed using tools like GCViewer or web-based services such as GCEasy.

Tags: JVM java garbage collection Performance Tuning Arthas

Posted on Sat, 29 Aug 2026 16:44:13 +0000 by okuto1973