Skip to content

Instantly share code, notes, and snippets.

@pedes
Created November 30, 2021 12:04
Show Gist options
  • Save pedes/e15f90d7420957c1e41483e965af342c to your computer and use it in GitHub Desktop.
Save pedes/e15f90d7420957c1e41483e965af342c to your computer and use it in GitHub Desktop.
public class MemoryInfoComponent {
private static final long MB = 1048576L;
public static Map<String, String> getMemoryStats() throws Exception {
Runtime rt = Runtime.getRuntime();
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
long usedMemory = rt.totalMemory() - rt.freeMemory();
Map<String, String> memoryStats = new HashMap<>();
memoryStats.put("JVM total memory", (rt.totalMemory() / 1048576L) + " MB");
memoryStats.put("JVM used memory", (usedMemory / 1048576L) + " MB");
memoryStats.put("JVM free memory", (rt.freeMemory() / 1048576L) + " MB");
memoryStats.put("Heap init size", (mbean.getHeapMemoryUsage().getInit() / 1048576L) + " MB");
memoryStats.put("Heap max size", (mbean.getHeapMemoryUsage().getMax() / 1048576L) + " MB");
memoryStats.put("Heap used size", (mbean.getHeapMemoryUsage().getUsed() / 1048576L) + " MB");
memoryStats.put("Heap commited", (mbean.getHeapMemoryUsage().getCommitted() / 1048576L) + " MB");
return memoryStats;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment