現在使用量: Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()
由兩次使用量計算出中間使用了多少記憶體
public class MemoryTester {
public static void main(String[] args) {
System.out.println("max memory:"
+ Runtime.getRuntime().maxMemory()
+ " bytes");
System.out.println("total memory:"
+ Runtime.getRuntime().totalMemory()
+ " bytes");
System.out.println("free memory:"
+ Runtime.getRuntime().freeMemory()
+ " bytes");
long memory = Runtime.getRuntime().totalMemory()
- Runtime.getRuntime().freeMemory();
ArrayList list = new ArrayList();
for (int i = 0; i < 10000; i++)
list.add(i);
System.out.println("max memory:"
+ Runtime.getRuntime().maxMemory()
+ " bytes");
System.out.println("total memory:"
+ Runtime.getRuntime().totalMemory()
+ " bytes");
System.out.println("free memory:"
+ Runtime.getRuntime().freeMemory()
+ " bytes");
memory = Runtime.getRuntime().totalMemory()
- Runtime.getRuntime().freeMemory() - memory;
System.out.println(memory + " bytes");
}
}
沒有留言:
張貼留言