Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/HeapDumper.java
41161 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.tools;2526import sun.jvm.hotspot.utilities.HeapHprofBinWriter;27import sun.jvm.hotspot.debugger.JVMDebugger;2829import java.io.IOException;3031/*32* This tool is used by the JDK jmap utility to dump the heap of the target33* process/core as a HPROF binary file. It can also be used as a standalone34* tool if required.35*/36public class HeapDumper extends Tool {3738private static String DEFAULT_DUMP_FILE = "heap.bin";3940private String dumpFile;4142public HeapDumper() {43this.dumpFile = DEFAULT_DUMP_FILE;44}4546public HeapDumper(String dumpFile) {47this.dumpFile = dumpFile;48}4950public HeapDumper(String dumpFile, JVMDebugger d) {51super(d);52this.dumpFile = dumpFile;53}5455@Override56public String getName() {57return "heapDumper";58}5960protected void printFlagsUsage() {61System.out.println(" <no option>\tto dump heap to " +62DEFAULT_DUMP_FILE);63System.out.println(" -f <file>\tto dump heap to <file>");64super.printFlagsUsage();65}6667// use HeapHprofBinWriter to write the heap dump68public void run() {69System.out.println("Dumping heap to " + dumpFile + " ...");70try {71new HeapHprofBinWriter().write(dumpFile);72System.out.println("Heap dump file created");73} catch (IOException ioe) {74System.err.println(ioe.getMessage());75}76}7778// JDK jmap utility will always invoke this tool as:79// HeapDumper -f <file> <args...>80public static void main(String args[]) {81HeapDumper dumper = new HeapDumper();82dumper.runWithArgs(args);83}8485public void runWithArgs(String... args) {86if (args.length > 2) {87if (args[0].equals("-f")) {88this.dumpFile = args[1];89String[] newargs = new String[args.length-2];90System.arraycopy(args, 2, newargs, 0, args.length-2);91args = newargs;92}93}9495execute(args);96}9798}99100101