1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 package sun.jvm.hotspot.tools; 26 27 import sun.jvm.hotspot.utilities.HeapHprofBinWriter; 28 import java.io.IOException; 29 30 /* 31 * This tool is used by the JDK jmap utility to dump the heap of the target 32 * process/core as a HPROF binary file. It can also be used as a standalone 33 * tool if required. 34 */ 35 public class HeapDumper extends Tool { 36 37 private static String DEFAULT_DUMP_FILE = "heap.bin"; 38 39 private String dumpFile; 40 41 public HeapDumper(String dumpFile) { 42 this.dumpFile = dumpFile; 43 } 44 45 protected void printFlagsUsage() { 46 System.out.println(" <no option>\tto dump heap to " + 47 DEFAULT_DUMP_FILE); 48 System.out.println(" -f <file>\tto dump heap to <file>"); 49 super.printFlagsUsage(); 50 } 51 52 // use HeapHprofBinWriter to write the heap dump 53 public void run() { 54 System.out.println("Dumping heap to " + dumpFile + " ..."); 55 try { 56 new HeapHprofBinWriter().write(dumpFile); 57 System.out.println("Heap dump file created"); 58 } catch (IOException ioe) { 59 System.err.println(ioe.getMessage()); 60 } 61 } 62 63 // JDK jmap utility will always invoke this tool as: 64 // HeapDumper -f <file> <args...> 65 public static void main(String args[]) { 66 String file = DEFAULT_DUMP_FILE; 67 if (args.length > 2) { 68 if (args[0].equals("-f")) { 69 file = args[1]; 70 String[] newargs = new String[args.length-2]; 71 System.arraycopy(args, 2, newargs, 0, args.length-2); 72 args = newargs; 73 } 74 } 75 76 HeapDumper dumper = new HeapDumper(file); 77 dumper.start(args); 78 dumper.stop(); 79 } 80 81 }