/** * Kicks off a poller thread that will periodically log human-readable NMT. * Uses {@link #formatNmt()} internally, and so also requires JVM argument * {@code -XX:NativeMemoryTracking=summary}. * @param interval The interval with which to poll and log NMT. * @return {@link NmtCloseable} that you can use to terminate the poller. */ public static NmtCloseable pollNmt(final Duration interval) { final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor("nmt-poller"); final long intervalNanos = TimeUnit.SECONDS.toNanos(interval.getSeconds()) + TimeUnit.NANOSECONDS.toNanos(interval.getNano()); final Runnable command = () -> { final String summary = formatNmt(); // null return values will cause a warning to get logged without us needing to do so. if (summary != null) { LOG.info("\n" + summary); } }; exec.scheduleWithFixedDelay(command, 0, intervalNanos, TimeUnit.NANOSECONDS); return exec::shutdownNow; }
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()); } }
/** * @see NmtTest */ @Test public void formatNmt() { Assert.assertNotNull(Memory.formatNmt()); }