Java Profiler Skill
Overview
Professional Java profiling skill integrating industry-leading tools for JVM performance analysis, memory management, and thread debugging in enterprise environments.
Capabilities
1. JVM Performance Analysis
- YourKit Java Profiler: Deep profiling for distributed systems
- JProfiler: Advanced UI with real-time metrics
- VisualVM: Built-in monitoring and profiling
- Java Flight Recorder (JFR): Low-overhead production profiling
2. Memory & Heap Analysis
- Heap dump analysis
- Memory leak detection
- Garbage collection tuning
- Object allocation tracking
3. Thread & Concurrency
- Thread contention detection
- Deadlock identification
- CPU sampling per thread
- Wait time analysis
4. Database & I/O
- JDBC query profiling
- Connection pool monitoring
- File I/O performance
- Network latency tracking
Usage
YourKit Profiler
# Attach to running JVM
java -agentpath:/path/to/yjpagent.so MyApplication
# CPU profiling
# Memory allocation tracking
# Thread monitoring
VisualVM (Built-in)
# Launch VisualVM
jvisualvm
# Connect to local/remote JVM
# Monitor: CPU, Memory, Threads, Classes
# Profile: CPU sampling, Memory profiling
Java Flight Recorder
# Start JFR recording
java -XX:StartFlightRecording=duration=60s,filename=recording.jfr MyApp
# Analyze with JMC
jmc recording.jfr
JProfiler Integration
# Remote profiling
java -agentpath:/path/to/jprofilerti.so=port=8849 MyApplication
# Connect JProfiler GUI to port 8849
Integration Scripts
jvm_monitor.sh
Automated JVM health monitoring:
#!/bin/bash
# Monitor JVM performance metrics
PID=$(jps | grep MyApplication | awk '{print $1}')
echo "=== JVM Memory Usage ==="
jmap -heap $PID
echo "=== Thread Dump ==="
jstack $PID
echo "=== GC Statistics ==="
jstat -gcutil $PID 1000 5
heap_analyzer.sh
Heap dump analysis automation:
#!/bin/bash
# Generate and analyze heap dump
PID=$1
DUMP_FILE="heap_dump_$(date +%Y%m%d_%H%M%S).hprof"
jmap -dump:live,format=b,file=$DUMP_FILE $PID
echo "Heap dump created: $DUMP_FILE"
# Analyze with jhat or VisualVM
jhat $DUMP_FILE
Performance Tuning Guidelines
JVM Arguments for Profiling
-XX:+UseG1GC # Use G1 garbage collector
-XX:MaxGCPauseMillis=200 # Target max GC pause
-Xms2g -Xmx4g # Heap size (min/max)
-XX:+HeapDumpOnOutOfMemoryError # Dump on OOM
-XX:HeapDumpPath=/tmp/heapdumps # Dump location
-XX:+PrintGCDetails # GC logging
-XX:+PrintGCDateStamps # Timestamp GC logs
Common Performance Issues
1. Memory Leaks
- Growing heap usage over time
- Increasing full GC frequency
- OutOfMemoryError exceptions
Detection: Monitor heap growth pattern, analyze retained objects
2. Thread Contention
- High CPU wait time
- Deadlocks and livelocks
- Poor scalability under load
Detection: Thread dumps, lock contention analysis
3. Database Performance
- N+1 query problems
- Missing connection pool configuration
- Unoptimized JDBC batch sizes
Detection: SQL logging, JDBC profiling
4. GC Overhead
- Long GC pause times
- Frequent full GCs
- High GC CPU usage
Detection: GC logs analysis, pause time distribution
Best Practices
- Start with Low-Overhead Tools: JFR for production, VisualVM for development
- Profile Realistic Workloads: Use production-like data volumes
- Focus on Hot Spots: Optimize methods consuming >10% CPU time
- Monitor GC Impact: Aim for <1% CPU time in GC
- Analyze Thread Dumps: Take 3-5 dumps, 10 seconds apart
- Use Sampling Mode initially, switch to instrumentation for deep analysis
Requirements
# Built-in tools (JDK 8+)
jvisualvm, jconsole, jmc, jstat, jmap, jstack, jhat
# Commercial tools (trial/licensed)
YourKit: https://www.yourkit.com/
JProfiler: https://www.ej-technologies.com/products/jprofiler/
Example Workflow
- Baseline: Capture performance metrics under normal load
- Profile: Run YourKit/JProfiler during load test
- Analyze: Identify CPU hot spots and memory allocations
- Optimize: Fix identified issues (algorithms, caching, pooling)
- Verify: Re-profile to confirm improvements
- Monitor: Set up continuous monitoring with JFR
Metrics to Track
- Throughput: Requests/second
- Latency: p50, p95, p99 response times
- Memory: Heap usage, allocation rate
- GC: Pause frequency and duration
- Threads: Active thread count, wait time
- CPU: Per-thread CPU time distribution