Java Heap Dump Analysis Primer

1,611 views

Published on

Java Heap Dump Analysis Primer

  • Be the first to comment

Java Heap Dump Analysis Primer

  1. 1. HEAP DUMP ANALYSIS BASICS
  2. 2. HEAP The Java virtual machine heap is the area of memory used by the JVM for dynamic memory allocation.
  3. 3. HEAP DUMP Ctrl-break -XX:+HeapDumpOnOutOfMemoryError generate a heap dump when an allocation from the Java heap or the permanent generation cannot be satisfied jmap -dump:format=b,file=dump.bin pid
  4. 4. JMAP ● Included with JDK ● On most systems (including PROD) ● Simple command line tool ● Provides heap dump, heap summary and more
  5. 5. JMAP HEAP DUMP $ jps -l -m 5918 GarbageCollection 5923 sun.tools.jps.Jps -l -m $ jmap -histo 5918|less num #instances #bytes class name ---------------------------------------------- 1: 76 9158320 [I 2: 35 8913488 [J 3: 34 8913440 [D 4: 139264 3342336 BannerUser 5: 1161 2329648 [C 6: 512 2316744 [B 7: 34 2228768 [F <... more stats ...> // Integer // Long // Double // Char // Byte // Float http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getName%28%29
  6. 6. $ sudo jmap -heap 5938 using thread-local object allocation. Mark Sweep Compact GC Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 199229440 (190.0MB) NewSize = 1310720 (1.25MB) MaxNewSize = 17592186044415 MB OldSize = 5439488 (5.1875MB) <...lots more stats...> HEAP SUMARY
  7. 7. JHAT HEAP ANALYSIS $ jmap -dump:format=b,file=dump.bin 5938 $ jhat ~/dump.bin
  8. 8. JHAT HEAP ANALYSIS
  9. 9. JHAT HEAP ANALYSIS
  10. 10. JCONSOLE
  11. 11. ECLIPSE MAT The Eclipse Memory Analyzer is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption. http://www.eclipse.org/mat/
  12. 12. STACK DUMP $ jstack -l 5722 <...shows you full stack trace...> Found one Java-level deadlock: ============================= "Thread-1": waiting to lock monitor 0x00007fa7dd004ab0 (object 0x00000007ef2b1020, a java.lang.Object), which is held by "Thread-0" "Thread-0": waiting to lock monitor 0x00007fa7dd007360 (object 0x00000007ef2b1030, a java.lang.Object), which is held by "Thread-1"
  13. 13. APPENDIX
  14. 14. GarbageCollection.java /** * GarbageCollection.java * Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/ */ class GarbageCollection { public static void main(String[] a) { int max = 10000; int min = 16; Object[] arr = new Object[min]; Runtime rt = Runtime.getRuntime(); System.out.println("Free/total memory:"); for (int m=0; m<max; m++) { for (int n=0; n<min-1; n++) arr[min-n-1] = arr[min-n-2]; arr[0] = getOneMega(); System.out.println(rt.freeMemory()+" "+rt.totalMemory()); try { Thread.sleep(1000/10); } catch (InterruptedException e) {
  15. 15. SimpleDeadLock.java /** * SimpleDeadLock.java * Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/ */ import java.util.*; public class SimpleDeadLock extends Thread { public static Object l1 = new Object(); public static Object l2 = new Object(); private int index; public static void main(String[] a) { Thread t1 = new Thread1(); Thread t2 = new Thread2(); t1.start(); t2.start(); } private static class Thread1 extends Thread { public void run() { synchronized (l1) {

×