Steps To Troubleshoot Typical Java Problems
This section list some typical java problems and the actions we need to perform, the tools we need to use to collect more information and/or to analyse and debug the problem.
- Hung, Deadlocked, or Looping Process
- Post-mortem Diagnostics
- Monitoring
- Actions on a Remote Debug Server
- Other Functions
Hung, Deadlocked, or Looping Process
- Print thread stack for all Java threads.
- Control-\
- Print thread trace for a process.
kill -QUITpidjstack -Fpid (with-Foption if pid does not respond)
- Detect deadlocks.
- JConsole tool, Threads tab
- Print information on deadlocked threads: Control-\
- Print lock information for a process:
jstack -lpid
- Get a heap histogram for a process.
- Start Java process with
-XX:+PrintClassHistogram, then Control-\ - Start Java process with
-XX:+PrintClassHistogram, thenkill -QUITpid jmap -histopid (with-Foption if pid does not respond)
- Start Java process with
- Dump Java heap for a process in binary format to file.
jmap -dump:format=b,file=filename pid (with-Foption if pid does not respond)
- Print shared object mappings for a process.
jmappid
- Print heap summary for a process.
jmap -heappid
- Print finalization information for a process.
jmap -finalizerinfopid
- Attach the command-line debugger to a process.
jdb -connect sun.jvm.hotspot.jdi.SAPIDAttachingConnector:pid=pid
- Attach the command-line debugger to a core file on the same machine.
jdb -connect sun.jvm.hotspot.jdi.SACoreAttachingConnector:javaExecutable=path,core=corefile
- Attach the command-line debugger to a core file on a different machine.
- On the machine with the core file:
jsadebugdpath corefile
and on the machine with the debugger:jdb -connect sun.jvm.hotspot.jdi.SADebugServerAttachingConnector:debugServerName=machine
- On the machine with the core file:
Post-mortem Diagnostics
- Examine the fatal error log file. Default file name is
hs_err_pidpid.login the working-directory. - Create a heap dump.
- HPROF:
java -agentlib:hprof=file=file,format=bapplication; then Control-\ - HPROF:
java -agentlib:hprof=heap=dumpapplication - JConsole tool, MBeans tab
- Start VM with
-XX:+HeapDumpOnOutOfMemoryError; if OutOfMemoryError is thrown, VM generates a heap dump.
- HPROF:
- Browse Java heap dump.
jhatheap-dump-file
- Dump Java heap from core file in binary format to a file.
jmap -dump:format=b,file=filename corefile
- Get a heap histogram from a core file.
jmap -histocorefile
- Print shared object mappings from a core file.
jmapcorefile
- Print heap summary ffrom a core file.
jmap -heapcorefile
- Print finalization information from a core file.
jmap -finalizerinfocorefile
- Print Java configuration information from a core file.
jinfocorefile
- Print thread trace from a core file.
jstackcorefile
- Print lock information from a core file.
jstack -lcorefile
Monitoring
- Print statistics on the class loader.
jstat -classvmID - Print statistics on the compiler.
- Compiler behavior:
jstat -compilervmID - Compilation method statistics:
jstat -printcompilationvmID
- Compiler behavior:
- Print statistics on garbage collection.
- Summary of statistics:
jstat -gcutilvmID - Summary of statistics, with causes:
jstat -gccausevmID - Behavior of the gc heap:
jstat -gcvmID - Capacities of all the generations:
jstat -gccapacityvmID - Behavior of the new generation:
jstat -gcnewvmID - Capacity of the new generation:
jstat -gcnewcapacityvmID - Behavior of the old and permanent generations:
jstat -gcoldvmID - Capacity of the old generation:
jstat -gcoldcapacityvmID - Capacity of the permanent generation:
jstat -gcpermcapacityvmID
- Summary of statistics:
- Monitor objects awaiting finalization
- JConsole tool, Summary tab
jmap -finalizerinfopidgetObjectPendingFinalizationCountmethod injava.lang.management.MemoryMXBeanclass
- Monitor memory
- Heap allocation profiles via HPROF:
java -agentlib:hprof=heap=sites - JConsole tool, Memory tab
- Control-\ prints generation information.
- Heap allocation profiles via HPROF:
- Monitor CPU usage.
- By thread stack:
java -agentlib:hprof=cpu=samplesapplication - By method:
java -agentlib:hprof=cpu=timesapplication - JConsole tool, Overview and Summary tabs
- By thread stack:
- Monitor thread activity: JConsole tool, Threads tab
- Monitor class activity: JConsole tool, Classes tab
Actions on a Remote Debug Server
First, attach the debug daemon jsadebugd, then execute the command.
- Dump Java heap in binary format to a file:
jmap -dump:format=b,file=filename hostID - Print shared object mappings:
jmaphostID - Print heap summary :
jmap -heaphostID - Print finalization information :
jmap -finalizerinfohostID - Print lock information :
jstack -lhostID - Print thread trace :
jstackhostID - Print Java configuration information:
jinfohostID
Other Functions
- Interface with the instrumented Java virtual machines.
- Monitor for the creation and termination of instrumented VMs:
jstatddaemon - List the instrumented VMs:
jps - Provide interface between remote mointoring tools and local VMs:
jstatddaemon
- Monitor for the creation and termination of instrumented VMs:
- Print Java configuration information from a running process.
jinfopid
- Dynamically set, unset, or change the value of certain Java VM flags for a process.
jinfo -flagflag
- Pass a Java VM flag to the virtual machine.
jhat -Jflag ...jmap -Jflag ...
- Print statistics of permanent generation of Java heap, by class loader.
jmap -permstat
- Report on monitor contention.
java -agentlib:hprof=monitor=yapplication
- Evaluate or execute a script in interactive or batch mode.
jrunscript
- Interface dynamically with an MBean, via JConsole tool, MBean tab:
- Show tree structure.
- Set an attribute value.
- Invoke an operation.
- Subscribe to notification.