Working with Debugging

1. Using IDE Debugger

FeatureUse
BreakpointsPause at line/method/exception
Step over/into/outNavigate execution
Watch / evaluateInspect/compute expressions
Hot-swapReload changed methods (limited)

2. Setting Breakpoints

TypeTriggers
LineSpecific line
ConditionalWhen expression true
ExceptionOn throw of class
Field watchpointRead/write of field
MethodEntry/exit

3. Using jdb Command Line Debugger

CommandUse
stop at Class:lineSet breakpoint
run / contStart / continue
step / next / step upNavigate
print exprEvaluate
whereStack trace

4. Remote Debugging

AspectDetail
JVM flag-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
IDEAttach to host:5005
Production cautionDon't expose JDWP publicly — RCE risk

5. Analyzing Thread Dumps

Example: Get thread dump

jcmd PID Thread.print
jstack PID
kill -3 PID    # SIGQUIT, dumps to stderr
Look ForIndicates
BLOCKED threadsLock contention
WAITING on same monitorHot lock
Found Java-level deadlockJVM detected deadlock
Many threads in same codeBottleneck

6. Analyzing Heap Dumps

ToolUse
jcmd PID GC.heap_dump file.hprofCapture
-XX:+HeapDumpOnOutOfMemoryErrorAuto on OOM
Eclipse MATAnalyze (find leak suspects)
VisualVMLighter analysis

7. Using Java Flight Recorder (JFR)

Example: JFR recording

jcmd PID JFR.start name=app duration=60s filename=app.jfr settings=profile
AspectDetail
Overhead< 1% with default settings
RecordsMethod profiling, GC, allocations, locks, I/O
AnalyzeJava Mission Control (JMC)

8. Using Java Mission Control (JMC)

TabShows
Method ProfilingHot methods (flame graph)
MemoryAllocations by class/method
GCPause times, throughput
Lock instancesContention

9. Reading Stack Traces

ElementDetail
Top frameWhere exception thrown
Caused byUnderlying root cause
SuppressedFrom try-with-resources
Helpful NullPointerExceptions JAVA 14+Identifies which variable was null

10. Logging Best Practices

PracticeDetail
SLF4J facadeDecouple from implementation
Parameterized messageslog.info("User {} logged in", id) avoids string concat
LevelsERROR / WARN / INFO / DEBUG / TRACE
Don't log secretsTokens, passwords, PII
Structured loggingJSON output for aggregation

11. Using Assertions

AspectDetail
Syntaxassert condition : "message";
Enable-ea JVM flag (off by default)
Use forInternal invariants, not input validation

12. Common Debugging Patterns

PatternWhen to Use
BisectionNarrow down failing change
Rubber duckExplain code to find issue
Minimal reproductionStrip to smallest failing case
Add logging strategicallyBoundaries between components
Check assumptionsPrint everything you "know"

13. Troubleshooting Performance Issues

SymptomFirst Steps
High CPUJFR profile + thread dump → identify hot methods
High memoryHeap dump + MAT → leak suspects
Long GC pausesGC logs (-Xlog:gc*) + tune collector
Slow responseJFR latency events; check DB/network
DeadlockThread dump shows it explicitly

14. Debugging Memory Issues

Tool/TechniqueUse
-XX:+HeapDumpOnOutOfMemoryErrorCapture at failure
Eclipse MAT "Leak Suspects"Auto-identify hot paths
Dominator treeFind what holds memory
Native Memory Tracking-XX:NativeMemoryTracking=summary

15. Debugging Concurrency Issues

IssueApproach
DeadlockThread dump → look for "Found one Java-level deadlock"
Race conditionJCStress for testing; logging with thread name
LivelockThreads RUNNABLE but no progress
StarvationJFR thread CPU time / lock contention events
Visibility bugsAudit volatile/synchronized; consider VarHandle