Keywords: JVM | heap memory monitoring | jstat command
Abstract: This article details how to monitor heap memory usage of a running JVM from the command line, specifically for scripting needs in environments without a graphical interface. Using the core tool jstat, combined with Java memory management principles, it provides practical examples and scripting methods to help developers effectively manage memory performance in application servers like Jetty. Based on Q&A data, with jstat as the primary tool and supplemented by other command techniques, the content ensures comprehensiveness and ease of implementation.
In Java application development, monitoring JVM heap memory usage is crucial for performance tuning and troubleshooting. Especially in environments without a graphical interface or on remote servers, command-line tools become indispensable. This article focuses on jstat as the core, exploring how to obtain actual heap memory usage data from a running JVM via the command line, rather than relying solely on the maximum allocation set by Xmx. It combines scenarios with Jetty application servers to offer practical scripting solutions.
JVM Memory Management Basics and Monitoring Needs
The heap memory of the Java Virtual Machine (JVM) is the primary area for object instance allocation, and its usage directly impacts application performance. Heap memory is typically divided into the young generation (including Eden and Survivor spaces) and the old generation, with utilization rates obtainable through garbage collection (GC) logs or monitoring tools. In real production environments, developers often need real-time monitoring of heap usage to prevent memory leaks or optimize GC strategies. For applications running on servers like Jetty, command-line monitoring is particularly important as it allows for automated script processing in windowless environments, such as triggering alerts based on memory thresholds or adjusting configurations.
Core Tool: Detailed Explanation of the jstat Command
jstat is a command-line tool provided by Oracle JDK for monitoring JVM statistics, including heap memory usage and GC activity. Its basic syntax is jstat -gc <pid>, where <pid> is the process ID of the target JVM. After executing this command, the output displays current usage of heap regions in kilobytes, such as S0U (Survivor space 0 utilization), S1U (Survivor space 1 utilization), EU (Eden space utilization), and OU (old generation utilization). This data reflects actual memory occupancy, not the upper limit set by Xmx, providing a basis for precise monitoring.
For example, on a running Jetty server, first obtain the process ID via jps or ps commands, then run jstat -gc 12345 (assuming PID is 12345). The output might resemble:
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
1024.0 1024.0 0.0 0.0 8192.0 4096.0 20480.0 10240.0 ...
Here, the EU and OU values represent the actual usage of the Eden and old generations, respectively, which developers can use directly to analyze memory trends.
Scripted Monitoring and Advanced Techniques
For automation, jstat output can be combined with other command-line tools. For instance, use the following command to calculate total heap usage in Java 8 (in kB):
jstat -gc <PID> | tail -n 1 | awk '{split($0,a," "); sum=a[3]+a[4]+a[6]+a[8]; print sum}'
This command parses jstat output, summing S0U, S1U, EU, and OU to get total heap usage. In Java 8 and above, to include metaspace and compressed class space usage, extend it by adding a[10] and a[12]. This approach facilitates integration into shell scripts for periodic checks or triggered actions, such as restarting services or sending notifications when usage exceeds thresholds.
Practical Case: Memory Monitoring for Jetty Servers
In Jetty application server deployments, memory monitoring is especially important as web applications often involve extensive object creation and GC activity. By scheduling regular jstat commands via cron jobs or monitoring systems, heap usage patterns can be tracked to identify memory leaks or optimize GC configurations. For example, write a script that collects heap data every 5 minutes and logs it:
#!/bin/bash
PID=$(jps | grep jetty | awk '{print $1}')
if [ -n "$PID" ]; then
USAGE=$(jstat -gc $PID | tail -n 1 | awk '{split($0,a," "); sum=a[3]+a[4]+a[6]+a[8]; print sum}')
echo "$(date): Heap usage: $USAGE kB" >> /var/log/jetty_memory.log
fi
This aids in long-term performance analysis and capacity planning.
Summary and Best Practices
jstat is a powerful and lightweight tool suitable for JVM memory monitoring in command-line environments. Combined with scripted processing, it effectively supports automated operations and performance management. Developers are advised to familiarize themselves with jstat output formats and customize monitoring strategies based on application needs. Also, note tool version compatibility, such as metaspace handling in Java 8. Through continuous monitoring and data analysis, application stability and resource utilization can be enhanced, particularly in server scenarios like Jetty.