1
我使用下面的實現來測試Java對象的內存消耗。
但是,當調用方法的for循環中i限制從384到1694時,它將打印用法爲104936 B。
它打印用法0B爲當上述我限值小於384在java中測試對象內存消耗的實現結果困惑
這是爲什麼?
- IDE:Eclipse的開普勒
- 的java:1.5(此行爲不同,當 Java版本也改變)
操作系統:Ubuntu的12.10
public static void main(String[] args) { Runtime runtime = Runtime.getRuntime(); long totalStart = runtime.totalMemory(); long start = runtime.freeMemory(); SampleTester sampleTester = new SampleTester(); sampleTester.callingMethod(); long totalEnd = runtime.totalMemory(); long end = runtime.freeMemory(); System.out.println("Usage [(("+totalEnd+"-"+end+") - ("+totalStart+"-"+start+"))] \t: " + ((totalEnd-end) - (totalStart-start))); } private void callingMethod(){ for(int i = 0; i < 1694; i++){ ArrayList<String> arrayList = new ArrayList<String>(); } }
可能重複[什麼是Java中的對象的內存消耗?](http://stackoverflow.com/questions/258120/what-is-the-memory-consumption-of-an-object-in- JAVA) – RamonBoza