publicstaticvoid main(String[] args) throws Exception { if (args.length == 0) {
usage(1); // no arguments
}
checkForUnsupportedOptions(args);
// the chosen option
String option = null;
// First iterate over the options (arguments starting with -). There should be // one. int optionCount = 0; while (optionCount < args.length) {
String arg = args[optionCount]; if (!arg.startsWith("-")) { break;
} if (arg.equals("-?") ||
arg.equals("-h") ||
arg.equals("--help") || // -help: legacy. Undocumented.
arg.equals("-help")) {
usage(0);
} else { if (option != null) {
usage(1); // option already specified
}
option = arg;
}
optionCount++;
}
// if no option provided then use default. if (option == null) {
usage(0);
}
// Next we check the parameter count. int paramCount = args.length - optionCount; if (paramCount != 1) {
usage(1);
}
String pidArg = args[1]; // Here we handle the built-in options // As more options are added we should create an abstract tool class and // have a table to map the options
ProcessArgumentMatcher ap = new ProcessArgumentMatcher(pidArg);
Collection<String> pids = ap.getVirtualMachinePids(JMap.class);
if (pids.isEmpty()) {
System.err.println("Could not find any processes matching : '" + pidArg + "'");
System.exit(1);
}
// Cast to HotSpotVirtualMachine as this is an // implementation specific method.
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm; try (InputStream in = hvm.executeCommand(command, args)) { // read to EOF and just print output byte b[] = newbyte[256]; int n; do {
n = in.read(b); if (n > 0) {
String s = new String(b, 0, n, UTF_8);
System.out.print(s);
}
} while (n > 0);
}
vm.detach();
}
privatestatic String parseFileName(String opt) { // opt starts with "file=" if (opt.length() > 5) { // pass whole "file=" string
String filename = opt.substring(5); try { // Get the canonical path - important to avoid just // passing a "heap.bin" and having the dump created // in the target VM working directory rather than the // directory where jmap is executed. returnnew File(filename).getCanonicalPath();
} catch (IOException ioe) { returnnull;
}
} // no filename returnnull;
}
for (String subopt : subopts) { if (subopt.equals("") || subopt.equals("all")) { // pass
} elseif (subopt.equals("live")) {
liveopt = "-live";
} elseif (subopt.startsWith("file=")) {
filename = parseFileName(subopt); if (filename == null) {
System.err.println("Fail: invalid option or no file name '" + subopt + "'");
usage(1);
}
} elseif (subopt.equals("format=b")) { // ignore format (not needed at this time)
} elseif (subopt.startsWith("gz=")) {
compress_level = subopt.substring("gz=".length()); if (compress_level.length() == 0) {
System.err.println("Fail: no number provided in option: '" + subopt + "'");
usage(1);
}
} else {
System.err.println("Fail: invalid option: '" + subopt + "'");
usage(1);
}
}
if (filename == null) {
System.err.println("Fail: invalid option or no file name");
usage(1);
}
System.out.flush();
// dumpHeap is not the same as jcmd GC.heap_dump
executeCommandForPid(pid, "dumpheap", filename, liveopt, compress_level);
}
privatestaticvoid checkForUnsupportedOptions(String[] args) { // Check arguments for -F, -m, and non-numeric value // and warn the user that SA is not supported anymore
int paramCount = 0;
for (String s : args) { if (s.equals("-F")) {
SAOptionError("-F option used");
}
if (s.equals("-heap")) {
SAOptionError("-heap option used");
}
/* Reimplemented using jcmd, output format is different fromoriginalone
if (paramCount > 1) {
SAOptionError("More than one non-option argument");
}
}
privatestaticvoid SAOptionError(String msg) {
System.err.println("Error: " + msg);
System.err.println("Cannot connect to core dump or remote debug server. Use jhsdb jmap instead");
System.exit(1);
}
// print usage message privatestaticvoid usage(int exit) {
System.err.println("Usage:");
System.err.println(" jmap -clstats <pid>");
System.err.println(" to connect to running process and print class loader statistics");
System.err.println(" jmap -finalizerinfo <pid>");
System.err.println(" to connect to running process and print information on objects awaiting finalization");
System.err.println(" jmap -histo[:[<histo-options>]] <pid>");
System.err.println(" to connect to running process and print histogram of java object heap");
System.err.println(" jmap -dump:<dump-options> <pid>");
System.err.println(" to connect to running process and dump java heap");
System.err.println(" jmap -? -h --help");
System.err.println(" to print this help message");
System.err.println("");
System.err.println(" dump-options:");
System.err.println(" live dump only live objects (takes precedence if both \"live\" and \"all\" are specified)");
System.err.println(" all dump all objects in the heap (default if one of \"live\" or \"all\" is not specified)");
System.err.println(" format=b binary format");
System.err.println(" file=<file> dump heap to <file>");
System.err.println(" gz=<number> If specified, the heap dump is written in gzipped format using the given compression level.");
System.err.println(" 1 (recommended) is the fastest, 9 the strongest compression.");
System.err.println("");
System.err.println(" Example: jmap -dump:live,format=b,file=heap.bin <pid>");
System.err.println("");
System.err.println(" histo-options:");
System.err.println(" live count only live objects (takes precedence if both \"live\" and \"all\" are specified)");
System.err.println(" all count all objects in the heap (default if one of \"live\" or \"all\" is not specified)");
System.err.println(" file=<file> dump data to <file>");
System.err.println(" parallel=<number> Number of parallel threads to use for heap inspection:");
System.err.println(" 0 (the default) means let the VM determine the number of threads to use");
System.err.println(" 1 means use one thread (disable parallelism).");
System.err.println(" For any other value the VM will try to use the specified number of threads, but might use fewer.");
System.err.println("");
System.err.println(" Example: jmap -histo:live,file=/tmp/histo.data <pid>");
System.exit(exit);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.