Created
March 19, 2019 19:54
-
-
Save jerolba/6a4c1e8e30b18d8d43cdb74ba480db07 to your computer and use it in GitHub Desktop.
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
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); | |
MemoryUsage beforeHeapMemoryUsage = mbean.getHeapMemoryUsage(); | |
Object instance = codeThatCreatesComplexDataStructure(); | |
MemoryUsage afterHeapMemoryUsage = mbean.getHeapMemoryUsage(); | |
long consumed = afterHeapMemoryUsage.getUsed() - | |
beforeHeapMemoryUsage.getUsed(); | |
System.out.println("Total consumed Memory:" + consumed); |
If you go to JDK source code documentation you can see that getUsed() method returns consumed mermory in bytes:
Thanks 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prints consumed memory in KB's or MB's ??