suraj tripathi
Greenhorn
Posts: 8
I want to know how and how much memory is allocated to a multi-dimensional array...Like,if i declare a 2*2 array of int then will only 16 bytes of total memory be allocated??

Bert Bates
author
Sheriff
Posts: 8898
5
Suraj -

Let's call this an intermediate level question

Bert

Rob Spoor
Sheriff
Posts: 20546
56
Although I believe the exact amount of memory is unspecified, it is most certainly larger than 4*4 bytes. To start, there are also the final "length" fields. In a 2x2 array there are 3 such fields: one for the outer array, and one for each inner array. So that's already a total of 4+3=7 integers. There is bound to be more overhead to.

Henry Wong
author
Marshal
Posts: 21190
80
In a 2x2 array there are 3 such fields: one for the outer array, and one for each inner array. So that's already a total of 4+3=7 integers. There is bound to be more overhead to.

The "overhead" that Rob refers to is the object header information. This may be different per JVM, for example, for the Windows JVM running in my Eclipse, it looks like it is 16 bytes. So in my case, I have 16 times 3 more bytes.

Furthermore, the "outer array" doesn't hold ints, it hold reference to the other two arrays. For 32 bit JVMs, these references are 4 byte each, double that for 64 bit JVMs, so this adds up to 2 times 4-8 bytes, or another 8 to 16 bytes just for the references.

Henry

suraj tripathi
Greenhorn
Posts: 8
Thank you all for your precious replies.....
Being a beginner, not having programming experience i am failing to understand what you have written(my programming experience is just 4 chapters of head first java book).So please,if you can elaborate on your answer, it would be helpful.

Thanks again.

fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Basically, the above reads to me that the correct answer is 'it depends'. What specific JVM you use has an impace as much as anything.

What Henry is saying (and he will correct me if I am wrong) is that when an object is created, aside from whatever storage is needed for the actual data, there is space needed for meta-data - information about the object. That is the 'object header' info he talks about.

Also, java doesn't really have multidimensional arrays, but only 1-d arrays. When you declare what you think of as a 2-d array, java sees a 1 dimensional array that holds things. Now, the things it holds happen to be... arrays. I like to think of it like shipping items. Say you're shipping eggs. Eggs go into a carton, and the cartons go into a shipping box. to get to a specific egg, you refer to the box, which carton in the box, and which position in the carton. To get to a specific value, you refer to the array of arrays, then to a specific array inside, then a specific spot inside that array.

Monu Tripathi
Rancher
Posts: 1369
1
Henry Wong wrote:
The "overhead" that Rob refers to is the object header information. This may be different per JVM, for example, for the Windows JVM running in my Eclipse, it looks like it is 16 bytes.

How can we deduce such an information(size of object header)?

P.S: sorry for hijacking the post(if I am doing any such thing)....

Wendy Gibbons
Bartender
Posts: 1111
• 1
suraj tripathi wrote:Thank you all for your precious replies.....
Being a beginner, not having programming experience i am failing to understand what you have written(my programming experience is just 4 chapters of head first java book).So please,if you can elaborate on your answer, it would be helpful.

Thanks again.

My Answer is if you are only that level of beginner don't care. and if the amount of bytes is important don't use java