Debugging and Troubleshooting

1. Using JDWP (Java Debug Wire Protocol)

AspectDetail
ProtocolWire format between debugger and JVM
JDIJava Debug Interface (debugger side)
JVMTINative back-end on JVM side
Transportsdt_socket, dt_shmem (Windows)

2. Setting Breakpoints

TypeUse
Line breakpointPause at line
Method breakpointEnter / exit
Field watchpointOn read / write
Exception breakpointOn throw / uncaught
ConditionalExpression must be true
Hit countTrigger after N hits

3. Using Remote Debugging (-agentlib:jdwp)

Example: Enable remote debug

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar app.jar
OptionUse
server=yJVM listens
suspend=yWait for debugger before main
address=*:5005Bind any iface (Java 9+ requires *)
Warning: JDWP allows arbitrary code execution. NEVER expose port to untrusted networks. Bind to localhost in production diagnostics or tunnel via SSH.

4. Analyzing Stack Traces

ElementMeaning
Caused byUnderlying exception
SuppressedFrom try-with-resources close
at A.b(File.java:42)Frame: class.method(source:line)
N moreIdentical tail elided
Helpful NPE (Java 14+)Pinpoints null subexpression

5. Using Logging Frameworks

FrameworkUse
SLF4JFacade — code against
LogbackRecommended impl
Log4j 2Async + GC-free modes
JUL (java.util.logging)Built-in but limited
System.LoggerJDK-neutral facade (Java 9+)

6. Enabling Assertions (-ea flag)

FlagUse
-eaEnable all (incl. user code)
-daDisable
-ea:com.acme...Enable for package
-esaEnable system assertions
Note: Assertions are off by default at runtime. Use only for invariants — never for argument validation in public APIs.

7. Using System.Logger API (Java 9+)

Example: System.Logger

private static final System.Logger LOG = System.getLogger(Foo.class.getName());
LOG.log(System.Logger.Level.INFO, "Started: {0}", id);
AspectDetail
Provider lookupServiceLoader: System.LoggerFinder
DefaultWraps JUL if no finder
UseJDK / lib code that wants to honor host logging stack

8. Debugging Multithreaded Applications

ToolUse
Thread dumpSnapshot all stacks; find BLOCKED/WAITING
Deadlock detectionjcmd PID Thread.print reports cycles
JFR Lock ProfilingContention hotspots
tsan / Java Concurrency Stress (jcstress)Race tests

9. Using Memory Profilers

ProfilerUse
Eclipse MATHeap dump analysis (Dominator Tree, Leak Suspects)
VisualVMLive JMX + sampling
JFR alloc eventsSample allocation stacks
async-profiler -e allocAllocation flame graph

10. Detecting Performance Bottlenecks

Bottleneck Diagnosis Workflow

  1. Measure baseline: throughput, p99 latency, CPU, GC %, allocation rate
  2. Identify resource: CPU-bound vs lock-bound vs I/O-bound vs GC-bound
  3. Profile with JFR (low overhead) or async-profiler (flame graph)
  4. Correlate with logs and metrics around the time window
  5. Fix top offender; re-measure; iterate
SymptomLikely Cause
High GC %Allocation pressure / heap too small
Many BLOCKED threadsLock contention on shared monitor
Low CPU + slowI/O wait or external service
Spiky latencyStop-the-world events

11. Understanding Native Stack Traces

SourceDetail
Crashhs_err_pid<PID>.log
SectionsSignal info, registers, native frames, Java frames
JVMCRASH causesBad JNI, hardware, JIT bug, OOM-killer
Toolsaddr2line, JIT-aware perf maps

12. Using Debug Visualization Tools

ToolUse
JDK Mission ControlJFR analysis UI
VisualVMLive monitoring + sampler
IntelliJ IDEA DebuggerInteractive stepping, smart step into, evaluate-expression
Flame graphsasync-profiler / Brendan Gregg's stackcollapse
GCViewer / GCEasyGC log visualization
WiresharkNetwork-level diagnostics

Summary

This cheatsheet covers Java 21+ features end-to-end: functional programming (lambdas, Streams, Optional), modern language features (records, sealed classes, pattern matching, text blocks, virtual threads, structured concurrency), runtime APIs (NIO.2, HttpClient, Modules, Class Loading, Instrumentation), JVM internals (memory, GC, JIT, JFR), performance methodology (JMH), security (JCE, TLS, secure coding), Project Panama (FFM API), and troubleshooting workflows. Always prefer modern, safe alternatives (FFM over Unsafe, Cleaner over finalize, virtual threads over thread pools for blocking I/O, ObjectInputFilter for serialization).