Created
May 16, 2014 11:55
-
-
Save hanbei/1c80555a1a211fbb6609 to your computer and use it in GitHub Desktop.
Memory Analyzer
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.io.PrintWriter; | |
public class MemoryStatistics { | |
private static final int MB = 1024 * 1024; | |
private static Runtime runtime = Runtime.getRuntime(); | |
public static long getUsedMemory() { | |
return (runtime.totalMemory() - runtime.freeMemory()) / MB; | |
} | |
public static long getFreeMemory() { | |
return runtime.freeMemory() / MB; | |
} | |
public static long getMaxAvailableMemory() { | |
return runtime.maxMemory() / MB; | |
} | |
public static long getAvailableMemory() { | |
return runtime.totalMemory() / MB; | |
} | |
public static void printMemoryUsage(final PrintWriter writer) { | |
writer.write(usageAsString()); | |
} | |
private static String usageAsString() { | |
StringBuilder buffer = new StringBuilder(); | |
buffer | |
.append("Used: ") | |
.append(getUsedMemory()) | |
.append(" Free: ") | |
.append(getFreeMemory()) | |
.append(" Available: ") | |
.append(getAvailableMemory()) | |
.append(" MaxAvailable ") | |
.append(getMaxAvailableMemory()); | |
return buffer.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment