147 148 private static String parseFileName(String opt) { 149 // opt starts with "file=" 150 if (opt.length() > 5) { 151 // pass whole "file=" string 152 String filename = opt.substring(5); 153 try { 154 // Get the canonical path - important to avoid just 155 // passing a "heap.bin" and having the dump created 156 // in the target VM working directory rather than the 157 // directory where jmap is executed. 158 return new File(filename).getCanonicalPath(); 159 } catch (IOException ioe) { 160 return null; 161 } 162 } 163 // no filename 164 return null; 165 } 166 167 private static void histo(String pid, String options) 168 throws AttachNotSupportedException, IOException, 169 UnsupportedEncodingException { 170 String liveopt = "-all"; 171 String filename = null; 172 String subopts[] = options.split(","); 173 174 for (int i = 0; i < subopts.length; i++) { 175 String subopt = subopts[i]; 176 if (subopt.equals("") || subopt.equals("all")) { 177 // pass 178 } else if (subopt.equals("live")) { 179 liveopt = "-live"; 180 } else if (subopt.startsWith("file=")) { 181 filename = parseFileName(subopt); 182 if (filename == null) { 183 usage(1); // invalid options or no filename 184 } 185 } else { 186 usage(1); 187 } 188 } 189 190 System.out.flush(); 191 192 // inspectHeap is not the same as jcmd GC.class_histogram 193 executeCommandForPid(pid, "inspectheap", liveopt, filename); 194 } 195 196 private static void dump(String pid, String options) 197 throws AttachNotSupportedException, IOException, 198 UnsupportedEncodingException { 199 200 String subopts[] = options.split(","); 201 String filename = null; 202 String liveopt = "-all"; 203 204 for (int i = 0; i < subopts.length; i++) { 205 String subopt = subopts[i]; 206 if (subopt.equals("live")) { 207 liveopt = "-live"; 208 } else if (subopt.startsWith("file=")) { 209 filename = parseFileName(subopt); 210 } 211 } 212 213 if (filename == null) { 270 System.err.println(" to connect to running process and print information on objects awaiting finalization"); 271 System.err.println(" jmap -histo[:[<histo-options>]] <pid>"); 272 System.err.println(" to connect to running process and print histogram of java object heap"); 273 System.err.println(" jmap -dump:<dump-options> <pid>"); 274 System.err.println(" to connect to running process and dump java heap"); 275 System.err.println(" jmap -? -h --help"); 276 System.err.println(" to print this help message"); 277 System.err.println(""); 278 System.err.println(" dump-options:"); 279 System.err.println(" live dump only live objects"); 280 System.err.println(" all dump all objects in the heap (default if one of \"live\" or \"all\" is not specified"); 281 System.err.println(" format=b binary format"); 282 System.err.println(" file=<file> dump heap to <file>"); 283 System.err.println(""); 284 System.err.println(" Example: jmap -dump:live,format=b,file=heap.bin <pid>"); 285 System.err.println(""); 286 System.err.println(" histo-options:"); 287 System.err.println(" live count only live objects"); 288 System.err.println(" all count all objects in the heap (default if one of \"live\" or \"all\" is not specified)"); 289 System.err.println(" file=<file> dump data to <file>"); 290 System.err.println(""); 291 System.err.println(" Example: jmap -histo:live,file=/tmp/histo.data <pid>"); 292 System.exit(exit); 293 } 294 } | 147 148 private static String parseFileName(String opt) { 149 // opt starts with "file=" 150 if (opt.length() > 5) { 151 // pass whole "file=" string 152 String filename = opt.substring(5); 153 try { 154 // Get the canonical path - important to avoid just 155 // passing a "heap.bin" and having the dump created 156 // in the target VM working directory rather than the 157 // directory where jmap is executed. 158 return new File(filename).getCanonicalPath(); 159 } catch (IOException ioe) { 160 return null; 161 } 162 } 163 // no filename 164 return null; 165 } 166 167 private static String add_option(String cmd, String opt) { 168 if (cmd.isEmpty()) { 169 return opt; 170 } 171 return cmd + "," + opt; 172 } 173 174 private static void histo(String pid, String options) 175 throws AttachNotSupportedException, IOException, 176 UnsupportedEncodingException { 177 String liveopt = "-all"; 178 String filename = null; 179 String parallelthreadnum = null; 180 String subopts[] = options.split(","); 181 boolean set_all = false; 182 boolean set_live = false; 183 String cmdline = ""; 184 185 for (int i = 0; i < subopts.length; i++) { 186 String subopt = subopts[i]; 187 if (subopt.equals("") || subopt.equals("all")) { 188 cmdline = add_option(cmdline, "-all"); 189 set_all = true; 190 } else if (subopt.equals("live")) { 191 // Add '-' for compatibility. 192 cmdline = add_option(cmdline, "-live"); 193 set_live = true; 194 } else if (subopt.startsWith("file=")) { 195 filename = parseFileName(subopt); 196 if (filename == null) { 197 usage(1); // invalid options or no filename 198 } 199 cmdline = add_option(cmdline, filename); 200 } else if (subopt.startsWith("parallelThreadNum=")) { 201 parallelthreadnum = subopt.substring(19); 202 if (parallelthreadnum == null) { 203 usage(1); 204 } 205 // Add "parallelThreadsNum=<>" for later check 206 cmdline = add_option(cmdline, subopt); 207 } else { 208 usage(1); 209 } 210 } 211 if (set_live && set_all) { 212 usage(1); 213 } 214 215 System.out.flush(); 216 // inspectHeap is not the same as jcmd GC.class_histogram 217 executeCommandForPid(pid, "inspectheap", cmdline); 218 } 219 220 private static void dump(String pid, String options) 221 throws AttachNotSupportedException, IOException, 222 UnsupportedEncodingException { 223 224 String subopts[] = options.split(","); 225 String filename = null; 226 String liveopt = "-all"; 227 228 for (int i = 0; i < subopts.length; i++) { 229 String subopt = subopts[i]; 230 if (subopt.equals("live")) { 231 liveopt = "-live"; 232 } else if (subopt.startsWith("file=")) { 233 filename = parseFileName(subopt); 234 } 235 } 236 237 if (filename == null) { 294 System.err.println(" to connect to running process and print information on objects awaiting finalization"); 295 System.err.println(" jmap -histo[:[<histo-options>]] <pid>"); 296 System.err.println(" to connect to running process and print histogram of java object heap"); 297 System.err.println(" jmap -dump:<dump-options> <pid>"); 298 System.err.println(" to connect to running process and dump java heap"); 299 System.err.println(" jmap -? -h --help"); 300 System.err.println(" to print this help message"); 301 System.err.println(""); 302 System.err.println(" dump-options:"); 303 System.err.println(" live dump only live objects"); 304 System.err.println(" all dump all objects in the heap (default if one of \"live\" or \"all\" is not specified"); 305 System.err.println(" format=b binary format"); 306 System.err.println(" file=<file> dump heap to <file>"); 307 System.err.println(""); 308 System.err.println(" Example: jmap -dump:live,format=b,file=heap.bin <pid>"); 309 System.err.println(""); 310 System.err.println(" histo-options:"); 311 System.err.println(" live count only live objects"); 312 System.err.println(" all count all objects in the heap (default if one of \"live\" or \"all\" is not specified)"); 313 System.err.println(" file=<file> dump data to <file>"); 314 System.err.println(" parallelThreadNum=<number> parallel threads number for heap iteration:"); 315 System.err.println(" parallelThreadNum=0 default behavior, use predefined number of threads"); 316 System.err.println(" parallelThreadNum=1 disable parallel heap iteration"); 317 System.err.println(" parallelThreadNum=<N> use N threads for parallel heap iteration"); 318 System.err.println(""); 319 System.err.println(" Example: jmap -histo:live,file=/tmp/histo.data <pid>"); 320 System.exit(exit); 321 } 322 } |