Skip to content

Instantly share code, notes, and snippets.

@cogumbreiro
Created October 14, 2016 23:40
Show Gist options
  • Save cogumbreiro/d7b2d907f900d7aea3f6334a1fe1f36c to your computer and use it in GitHub Desktop.
Save cogumbreiro/d7b2d907f900d7aea3f6334a1fe1f36c to your computer and use it in GitHub Desktop.
Sample Java's memory usage.
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PrintMemoryUsage implements Runnable {
private final Logger log = Logger.getLogger("GORN");
private final Level level;
private PrintMemoryUsage(Level level) {
this.level = level;
}
public void run() {
Runtime rt = Runtime.getRuntime();
long usedMemory = rt.totalMemory() - rt.freeMemory();
log.log(level, "MEMORY USED " + usedMemory);
}
public static void printUsage(Level level, int period, TimeUnit unit) {
ScheduledExecutorService s = Executors.newScheduledThreadPool(1);
s.scheduleAtFixedRate(new PrintMemoryUsage(level), 0, period, unit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment