I use the below implemenatation to test the memory consumption of the java objects.
But it prints usage as 104936 B when the i limit is from 384 to 1694 in the calling method's for loop.
It prints usage as 0B for when the above i limit is less than 384.
Why is this?
- IDE : eclipse kepler
- java : 1.5 (this acts differently when the java version is also changed)
OS : 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>(); } }