public class RuntimeMemoryUsageDemo {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
float max = rt.maxMemory() / 1024 / 1024; //convert bytes into MB
System.out.println("Total "+max + " MB");
long start = rt.freeMemory();
StringBuffer sb = new StringBuffer("Hello");
for (int i = 1; i <= 5000; i++) {
sb.append("Testing");
}
long end = rt.freeMemory();
float used = (start - end);
System.out.println("Used memory: " + used + " Bytes");
rt.gc();//request to garbage collector
//System.gc();
}
}
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
float max = rt.maxMemory() / 1024 / 1024; //convert bytes into MB
System.out.println("Total "+max + " MB");
long start = rt.freeMemory();
StringBuffer sb = new StringBuffer("Hello");
for (int i = 1; i <= 5000; i++) {
sb.append("Testing");
}
long end = rt.freeMemory();
float used = (start - end);
System.out.println("Used memory: " + used + " Bytes");
rt.gc();//request to garbage collector
//System.gc();
}
}
Comments
Post a Comment