How much actual memory does an array consume

Memory usage of a single-dimension array

A single-dimension array is a single object. As expected, the array has the usual object header. However, this object head is 12 bytes to accommodate a four-byte array length. Then comes the actual array data which, as you might expect, consists of the number of elements multiplied by the number of bytes required for one element, depending on its type. The memory usage for one element is 4 bytes for an object reference If the total memory usage of the array is not a multiple of 8 bytes, then the size is rounded up to the next mutlitple of 8 (just as for any other object).

 

Memory usage of a two-dimensional array

In a language such as C, a two-dimensional array (or indeed any multidimensional array) is essentially a one-dimensional array with judicious pointer manipulation. This is not the case in Java, where a multidimensional array is actually a set of nested arrays. This means that every row of a two-dimensional array has the overhead of an object, since it actually is a separate object!

For example, let’s consider a 10×10 int array. Firstly, the “outer” array has its 12-byte object header followed by space for the 10 elements. Those elements are object references to the 10 arrays making up the rows. That comes to 12+4*10=52 bytes, which must then be rounded up to the next multiple of 8, giving 56. Then, each of the 10 rows has its own 12-byte object header, 4*10=40 bytes for the actual row of ints, and again, 4 bytes of padding to bring the total for that row to a multiple of 8. So in total, that gives 11*56=616 bytes. That’s a bit bigger than if you’d just counted on 10*10*4=400 bytes for the hundred “raw” ints themselves.

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *