When dealing with third-party Java applications where startup parameters are hard to verify, using the jps
tool to confirm the actually effective JVM parameters is one of the most effective diagnostic methods. Here’s a step-by-step guide:
1. Basic Process Identification
jps <span>-l</span> <span>-m</span> <span>-v</span>jps <span>-l</span> <span>-m</span> <span>-v</span>jps -l -m -v
Enter fullscreen mode Exit fullscreen mode
- Output Example:
12345 app.jar -Xmx2g -Dspring.profiles.active=prod67890 com.example.MainClass -Dconfig.path=/opt/conf12345 app.jar -Xmx2g -Dspring.profiles.active=prod 67890 com.example.MainClass -Dconfig.path=/opt/conf12345 app.jar -Xmx2g -Dspring.profiles.active=prod 67890 com.example.MainClass -Dconfig.path=/opt/conf
Enter fullscreen mode Exit fullscreen mode
- Parameter Breakdown:
-
-l
: Displays the full package name or JAR path. -
-m
: Outputs parameters passed to the main method. -
-v
: Shows JVM arguments.
-
2. Comparing Key Parameters
Expected Parameter | JPS Verification Command | Common Issue Scenarios |
---|---|---|
Heap Memory Configuration | `jps -v | grep -E ‘Xmx |
GC Algorithm Selection | {% raw %}`jps -v | grep ‘Use.*GC’` |
System Properties | `jps -v | grep ‘Dproperty=’` |
Logging Configuration | `jps -v | grep ‘logging.config’` |
3. Advanced Diagnostic Techniques
3.1 Process Filtering
<span># Finding a specific application process</span>jps <span>-v</span> | <span>grep</span> <span>'app-name-pattern'</span><span># Real-time monitoring with pipes</span>watch <span>-n</span> 1 <span>"jps -l -v | grep -v sun.tools.jps"</span><span># Finding a specific application process</span> jps <span>-v</span> | <span>grep</span> <span>'app-name-pattern'</span> <span># Real-time monitoring with pipes</span> watch <span>-n</span> 1 <span>"jps -l -v | grep -v sun.tools.jps"</span># Finding a specific application process jps -v | grep 'app-name-pattern' # Real-time monitoring with pipes watch -n 1 "jps -l -v | grep -v sun.tools.jps"
Enter fullscreen mode Exit fullscreen mode
3.2 Parameter Tracing
<span># Locate the startup script path</span>ps <span>-ef</span> | <span>grep </span>java | <span>grep</span> <span>-v</span> <span>grep</span> | <span>awk</span> <span>'{print $NF}'</span><span># Locate the startup script path</span> ps <span>-ef</span> | <span>grep </span>java | <span>grep</span> <span>-v</span> <span>grep</span> | <span>awk</span> <span>'{print $NF}'</span># Locate the startup script path ps -ef | grep java | grep -v grep | awk '{print $NF}'
Enter fullscreen mode Exit fullscreen mode
3.3 Dynamic Verification
jinfo <span>-flags</span> <PID> 2>/dev/null | <span>grep</span> <span>'specific_param'</span>jinfo <span>-flags</span> <PID> 2>/dev/null | <span>grep</span> <span>'specific_param'</span>jinfo -flags <PID> 2>/dev/null | grep 'specific_param'
Enter fullscreen mode Exit fullscreen mode
- Requires permissions to the JDK installation path.
- Supports viewing non-standard parameters like CMS collector settings.
4. Handling Common Issues
Case 1: No Output from JPS
- Cause: Missing
tools.jar
in JRE environment. - Solution: Install a full JDK instead of just JRE.
Case 2: Incomplete Parameter Display
- Use combined commands:
jrunscript <span>-e</span> <span>'java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().forEach(print)'</span>jrunscript <span>-e</span> <span>'java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().forEach(print)'</span>jrunscript -e 'java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().forEach(print)'
Enter fullscreen mode Exit fullscreen mode
Case 3: Container Environment Limitations
- Inside containers, run:
docker <span>exec</span> <span>-it</span> <container> /path/to/jps <span>-v</span>docker <span>exec</span> <span>-it</span> <container> /path/to/jps <span>-v</span>docker exec -it <container> /path/to/jps -v
Enter fullscreen mode Exit fullscreen mode
- For Kubernetes:
kubectl <span>exec</span> <pod> <span>--</span> jps <span>-v</span>kubectl <span>exec</span> <pod> <span>--</span> jps <span>-v</span>kubectl exec <pod> -- jps -v
Enter fullscreen mode Exit fullscreen mode
5. Verification of Parameter Effectiveness
- Memory Verification:
jstat <span>-gc</span> <PID> 1000 5 <span># Monitor heap memory changes</span>jstat <span>-gc</span> <PID> 1000 5 <span># Monitor heap memory changes</span>jstat -gc <PID> 1000 5 # Monitor heap memory changes
Enter fullscreen mode Exit fullscreen mode
- GC Verification:
jstat <span>-gcutil</span> <PID> | <span>awk</span> <span>'{print $6}'</span> <span># Observe GC type</span>jstat <span>-gcutil</span> <PID> | <span>awk</span> <span>'{print $6}'</span> <span># Observe GC type</span>jstat -gcutil <PID> | awk '{print $6}' # Observe GC type
Enter fullscreen mode Exit fullscreen mode
- System Properties Verification:
jinfo <span>-sysprops</span> <PID> | <span>grep</span> <span>'property.key'</span>jinfo <span>-sysprops</span> <PID> | <span>grep</span> <span>'property.key'</span>jinfo -sysprops <PID> | grep 'property.key'
Enter fullscreen mode Exit fullscreen mode
By deeply utilizing the jps
tool chain, you can accurately identify issues like:
- Parameter typos (e.g.,
-Xmx2048m
misspelled as-Xmx2048
). - Multi-level parameter overriding in startup scripts (environment variables > command-line args > config files).
- Parameter invalidation due to container
cgroup
limits (like-Xmx
exceeding container memory). - Framework default parameters overriding custom settings (common in Spring Boot’s
application.properties
).
References:
tag
OOM
OutOfMemory
原文链接:Diagnosing JVM Parameters in Java Applications with JPS
暂无评论内容