This class offers a simple way to track the peak memory used by a program.
Simply launch a
PeakMemoryMonitor
as
Thread monitor = new Thread(new PeakMemoryMonitor());
monitor.start()
and then when you want to stop monitoring, call
monitor.interrupt();
monitor.join();
You only need the last line if you want to be sure the monitor stops before
you move on in the code; and strictly speaking, you should surround the
monitor.join()
call with a
try/catch
block, as
the
Thread
you are running could itself be interrupted, so you
should actually have something like
monitor.interrupt();
try {
monitor.join();
} catch (InterruptedException ex) {
// handle the exception
}
or else throw the exception.