/** * Requires JVM argument {@code -XX:NativeMemoryTracking=summary}. * Logs a warning if there was an error getting the NMT summary or if NMT was disabled. * This warning will be logged only once per process instance. * Produces simpler and more concise human-readable summary of NMT than the native human-readable output from the * JVM. * @return Human-readable NMT summary. null if there was an error getting the summary. * @see #getNmt() */ @Nullable public static String formatNmt() { final Nmt nmt = Nmt.get(); if (nmt == null) { return null; } return nmt.toString(); }
/** * Requires JVM argument {@code -XX:NativeMemoryTracking=summary}. * Logs a warning if there was an error getting the NMT summary or if NMT was disabled. * This warning will be logged only once per process instance. * @return null if there was an error getting the summary. */ @Nullable static Nmt get() { final String nmt = invoke(); if (nmt == null) { return null; } try { return parse(nmt); } catch (IllegalArgumentException e) { LOG.warn("un-parseable NMT data:\n{}", nmt, e); return null; } }
total = parseUsage(totalStr.substring(prefixTotal.length())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("could not parse total", e); final Usage usage; try { usage = parseUsage(s.substring(i + prefixParen.length())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(String.format("could not parse usage on line %d", line)); throw new IllegalArgumentException("no categories parsed"); return new Nmt(total, categories);
@Test public void parseUsage2() { final Nmt.Usage u = Nmt.parseUsage("reserved=69124KB, committed=26356KB)\n"); Assert.assertNotNull(u); Assert.assertEquals(u.reserved, 69124 * Nmt.K); Assert.assertEquals(u.committed, 26356 * Nmt.K); }
/** * Requires JVM argument {@code -XX:NativeMemoryTracking=summary}. * Logs a warning if there was an error getting the NMT summary or if NMT was disabled. * This warning will be logged only once per process instance. * @return {@link Nmt} instance. null if there was an error getting the summary. */ @Nullable public static Nmt getNmt() { return Nmt.get(); }
@Test(expected = IllegalArgumentException.class) public void parseTooFewLines() { Nmt.parse("a\nb"); }
public static void main(String[] args) { System.out.println("VM output:"); System.out.print(Nmt.invoke()); System.out.println("our parsed/formatted version:"); System.out.print(Memory.formatNmt()); } }
@Test public void parseUsage1() { final Nmt.Usage u = Nmt.parseUsage("reserved=76944KB, committed=48784KB"); Assert.assertNotNull(u); Assert.assertEquals(u.reserved, 76944 * Nmt.K); Assert.assertEquals(u.committed, 48784 * Nmt.K); }
@Test public void get() { validate(Nmt.get()); }
@Test public void parse() { final String s = "\n" + "Native Memory Tracking:\n" + "\n" + "Total: reserved=5710704KB, committed=471520KB\n" + "- Java Heap (reserved=4194304KB, committed=262144KB)\n" + " (mmap: reserved=4194304KB, committed=262144KB) \n" + " \n" + "- Class (reserved=1066181KB, committed=18885KB)\n" + " (classes #1722)\n" + " (malloc=9413KB #951) \n" + " (mmap: reserved=1056768KB, committed=9472KB) \n" + " \n" + "- Thread (reserved=20756KB, committed=20756KB)\n" + " (thread #20)\n" + " (stack: reserved=20480KB, committed=20480KB)\n" + " (malloc=60KB #110) \n" + " (arena=215KB #40)\n"; final Nmt nmt = Nmt.parse(s); validate(nmt); Assert.assertEquals(nmt.categories.size(), 3); Assert.assertEquals(nmt.categories.get("Class").committed, 18885 * Nmt.K); }
@Test public void parseUsage3() { final Nmt.Usage u = Nmt.parseUsage("reserved=69124KB, committed=26356KB)"); Assert.assertNotNull(u); Assert.assertEquals(u.reserved, 69124 * Nmt.K); Assert.assertEquals(u.committed, 26356 * Nmt.K); }