Created
October 14, 2016 23:40
-
-
Save cogumbreiro/d7b2d907f900d7aea3f6334a1fe1f36c to your computer and use it in GitHub Desktop.
Sample Java's memory usage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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