Skip to content

Instantly share code, notes, and snippets.

@hanbei
Created May 16, 2014 11:55
Show Gist options
  • Save hanbei/1c80555a1a211fbb6609 to your computer and use it in GitHub Desktop.
Save hanbei/1c80555a1a211fbb6609 to your computer and use it in GitHub Desktop.
Memory Analyzer
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