# Java Profiler

> Enterprise Java profiling for JVM applications, microservices, and distributed systems

- Skill: `lodetomasi/java-profiler` (Agent Skill)
- Install (CLI): `npx skillmds@latest add lodetomasi/java-profiler`
- Raw SKILL.md: https://api.skillmd.com/api/skills/lodetomasi/java-profiler/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: lodetomasi (https://skillmd.com/u/lodetomasi)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/lodetomasi/java-profiler

---


# 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
```bash
# Attach to running JVM
java -agentpath:/path/to/yjpagent.so MyApplication

# CPU profiling
# Memory allocation tracking
# Thread monitoring
```

### VisualVM (Built-in)
```bash
# Launch VisualVM
jvisualvm

# Connect to local/remote JVM
# Monitor: CPU, Memory, Threads, Classes
# Profile: CPU sampling, Memory profiling
```

### Java Flight Recorder
```bash
# Start JFR recording
java -XX:StartFlightRecording=duration=60s,filename=recording.jfr MyApp

# Analyze with JMC
jmc recording.jfr
```

### JProfiler Integration
```bash
# 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:
```bash
#!/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:
```bash
#!/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
```bash
-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

1. **Start with Low-Overhead Tools**: JFR for production, VisualVM for development
2. **Profile Realistic Workloads**: Use production-like data volumes
3. **Focus on Hot Spots**: Optimize methods consuming >10% CPU time
4. **Monitor GC Impact**: Aim for <1% CPU time in GC
5. **Analyze Thread Dumps**: Take 3-5 dumps, 10 seconds apart
6. **Use Sampling Mode** initially, switch to instrumentation for deep analysis

## Requirements

```bash
# 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

1. **Baseline**: Capture performance metrics under normal load
2. **Profile**: Run YourKit/JProfiler during load test
3. **Analyze**: Identify CPU hot spots and memory allocations
4. **Optimize**: Fix identified issues (algorithms, caching, pooling)
5. **Verify**: Re-profile to confirm improvements
6. **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

