0

Whether I use Long[], Integer[] or ArrayList<Integer>, why do all of them return the same memory usage?

System.out.println("Memory Usage : " + Runtime.getRuntime().totalMemory()/(1024*1024));
System.out.println("Memory Usage : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024));

//Long[] aa = new Long[70000000];
Integer[] bb = new Integer[70000000];
//ArrayList<Integer> a = new ArrayList<Integer>(70000000);

System.gc();

System.out.println("Memory Usage : " + Runtime.getRuntime().totalMemory()/(1024*1024));
System.out.println("Memory Usage : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024));
2
  • 3
    Because your measurement methods are completely inaccurate. System.gc() is "Hey, can you maybe GC at some point in the vaguely near future?" and Runtime.getRuntime().totalMemory() is a very rough approximation that can be wildly off. Commented Nov 30, 2012 at 15:35
  • 4
    What's this got to do with Spring and Hibernate? Commented Nov 30, 2012 at 15:36

1 Answer 1

6

In the above statements Whether i use Long[], Integer[] or ArrayList, why all of them give same memory usage ?

It makes perfect sense. In all cases, you're basically allocating an array which is 70000000 * (size of reference on your JVM). The size of an Integer reference is the same as the size of a Long reference, which is the same as the size of an Object reference. In the ArrayList case you've got the very small additional overhead of the ArrayList object itself, but that's pretty small - and easily explained as per Louis's comment.

If you actually populated those arrays, creating instances of Long and Integer, then you'd see a difference. Likewise if you created a long[] vs an int[], you'd see a difference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.