This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes question about memory allocation. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "question about memory allocation. " Watch "question about memory allocation. " New topic
Author

question about memory allocation.

suraj tripathi
Greenhorn

Joined: Feb 09, 2009
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

Joined: Oct 14, 2002
Posts: 8803
    
    5
Suraj -

Let's call this an intermediate level question

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

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



Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
suraj tripathi
Greenhorn

Joined: Feb 09, 2009
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

Joined: Oct 02, 2003
Posts: 11161
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Monu Tripathi
Rancher

Joined: Oct 12, 2008
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)....

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about memory allocation.
 
Similar Threads
How will I know how much memory can be used by my java application?
interview questions
Array doubt
Making object null
string pool