Java - JVM Diangostics

What Diagnosis can be done
  • Post-mortem diagnostics - After an application has crashed
    • Fatal Error - Crash Dump 
      • java -XX:ErrorFile=/var/log/java/hs_err_pid%p.log
      • java -XX:OnError = Option - The VM can optionally run a user specified script on fatal error. (send email)
      • java -XX:+ShowMessageBoxOnError - Shows a message box once a Fatal Error occurs.
  • Hung Processes - Investigate Hung/Deadlocked process.
    • Ctrl+Break / kill -QUIT - Performs a thread dump as well as Deadlock Detection. Prints list of concurrent locks and their owners as well as Heap Histogram
    • jdb - Attach debugger to a Java Process
    • jhat
    • jinfo
    • jmap
    • jstack
  • Monitoring - Monitor a running application
    • Java Visual VM
    • JConsole Utility
    • jstat - Provide information on performance and resource consumption
    • visualgc
  • Other tools & Options
    • HPROF Profiler - Writes profiling info to a file or socket in ascii or binary format.
      • CPU Usage
      • Heap Usage
      • Contention profiles
      • Useful in analysing Performance, lock contention & memory leaks.
      • java -agentlib:hprof ToBeProfiledClass
    • -verbose
      • -verbose:class - Enables logging of loading & unloading of class
      • -verbose:gc - Enables logging of Garbage Collection 
      • -verbose:jni - Enables logging of JNI.
  • Operating System tools - Specific for each OS.




Command Line Tools available in JDK for a running JVM

jps

The jps tool lists the instrumented HotSpot Java Virtual Machines (JVMs) on the target system. The tool is limited to reporting information on JVMs for which it has the access permissions.

jinfo

Prints Java configuration information for a given Java process or core file or a remote debug server

jinfo <PID>

Can be used to set VM Flags as follows

jinfo -flag <flag_name>=[01] <PID>

  • HeapDumpOnOutOfMemoryError
  • HeapDumpPath
  • PrintGC
  • PrintGCDetails
  • PrintGCTimeStamps
  • PrintClassHistogram
  • PrintConcurrentLocks
Generate a Stack Dump

jstack


  jstack [-l] <pid>
        (to connect to running process)
    jstack -F [-m] [-l] <pid>
        (to connect to a hung process)
    jstack [-m] [-l] <executable> <core>
        (to connect to a core file)
    jstack [-m] [-l] [server_id@]<remote server IP or hostname>
        (to connect to a remote debug server)

Monitoring JVM

jstat -<option> PID

jstat -options
-class
-compiler
-gc
-gccapacity
-gccause
-gcnew
-gcnewcapacity
-gcold
-gcoldcapacity
-gcpermcapacity
-gcutil
-printcompilation

jmap

jmap prints shared object memory maps or heap memory details of a given process or core file or a remote debug server.

jmap -heap <PID>

  • Generates the configuration of heap. Size of various generations & ratios.
  • Generates the current usage [ capacity | used | free | %used ]of Heap for each generation.


jmap -histo <PID>

  • Generates a histogram of all the classes currently loaded in the JVM
  • Number of Instances of each Class | Total bytes occupied by these instances


JConsole & VisualVm - Dynamic attach
Visual VM is latest & advanced. It has profiling capacity for memory & CPU

AppDynamics Lite - Agent Install

Using JProfiler and external tool we can integrate the code with the tool and from the tool jump to the exact location in the code that is allocated any given object(s).


Generate a Heap Dump

Via Java VM parameters:

-XX:+HeapDumpOnOutOfMemoryError writes heap dump on OutOfMemoryError (recommended)

-XX:+HeapDumpOnCtrlBreak writes heap dump together with thread dump on CTRL+BREAK

-agentlib:hprof=heap=dump,format=b combines the above two settings (old way; not recommended as the VM frequently dies after CTRL+BREAK with strange errors)

Via Tools:

From a running process
jmap -dump:format=b,file=/Users/ravi/work/temp/HeapDump.hprof  <PID>

From a Core Dump File
jmap -dump:format=b, file=HeapDump.hprof /path/to/bin/java core_dump_file

From Sun JConsole: Launch jconsole.exe and invoke operation dumpHeap() on HotSpotDiagnostic MBean. can be used to monitor the number of objects that are pending finalization.


Analyse a Heap Dump

jhat - Analyse the Heap Dump (Java Heap Analyser Tool)





Memory Leak

If your application's execution time becomes longer and longer, or if the operating system seems to be performing slower and slower, this could be an indication of a memory leak. In most cases it requires very detailed knowledge of the application. In addition the process is often iterative and lengthy. 

Obtaining a Heap Histogram on a Running Process

  • jmap -histo <pid>
  • -XX:+PrintClassHistogram



Analyse PermGen ( This area also holds internalized strings.)


jmap -permstat 29620

Attaching to process ID 29620, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 1.6.0-rc-b100
12674 intern Strings occupying 1082616 bytes.
...
please wait.. computing liveness.........................................done.
class_loader classes bytes parent_loader alive? type

<bootstrap> 1846 5321080 null live <internal>
0xd0bf3828 0 0 null live sun/misc/Launcher$ExtClassLoader@0xd8c98c78
0xd0d2f370 1 904 null dead sun/reflect/DelegatingClassLoader@0xd8c22f50
0xd0c99280 1 1440 null dead sun/reflect/DelegatingClassLoader@0xd8c22f50
0xd0b71d90 0 0 0xd0b5b9c0 live java/util/ResourceBundle$RBClassLoader@0xd8d042e8
0xd0d2f4c0 1 904 null dead sun/reflect/DelegatingClassLoader@0xd8c22f50





Heap Tuning Parameters

You can control the heap size with the following JVM parameters:

-Xmsvalue
-Xmxvalue
-XX:MinHeapFreeRatio=minimum
-XX:MaxHeapFreeRatio=maximum
-XX:NewRatio=ratio
-XX:NewSize=size
-XX:MaxNewSize=size
-XX:+AggressiveHeap
-XX:SurvivorRatio=6

The JVM grows or shrinks the heap at each GC to try to keep the proportion of free space to the living objects at each collection within a specific range. This range is set as a percentage by the parameters -XX:MinHeapFreeRatio=minimum and -XX:MaxHeapFreeRatio=maximum; and the total size bounded by -Xms and -Xmx.





If you only need very coarse time resolution such as (after startup, after 5 minutes, before manually triggering a bug, after manually triggering a bug etc), then you can take several dumps and compare them.

Otherwise, if you are able to add an agent and run the scenario again you can log this yourself. With your JDK in *$JAVA_HOME/demo/jvmti directory* there is a sample agent program heapTracker which can track object creation deletion. This could easily be modified to also include timestamp

http://www.oracle.com/technetwork/java/javase/tooldescr-136044.html#gbmls

---


Command-Line Options - / -X /-XX

- The vanilla options are supported for future versions

X - (Non Standard) - Not guaranteed to be supported on all implementations of VM. These are not guaranteed to be supported in future versions.

-XX - (Not Stable) - Subject to change without notice. Not recommended for casual use.

It is important to note that these -XX options are not part of the Java API and can vary from one release to the next.

 jmap -histo \ /java/re/javase/6/latest/binaries/solaris-sparc/bin/java core.27421

1. How many objects of each class are currently allocated
2. How much space each of these class-objects (combined) are taking.

Size      Count   Class description
-------------------------------------------------------
86683872  3611828 java.lang.String
20979136  204     java.lang.Object[]
403728    4225    * ConstMethodKlass
306608    4225    * MethodKlass
220032    6094    * SymbolKlass
152960    294     * ConstantPoolKlass
108512    277     * ConstantPoolCacheKlass
104928    294     * InstanceKlassKlass


On Solaris OS and Linux, the jmap -finalizerinfo option prints information on objects awaiting finalization.

No comments:

Post a Comment