Results 1 to 9 of 9
Thread: Array memory allocation
- 10-13-2009, 07:52 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
Array memory allocation
When you create an array of int's (not Integers), does java allocate an array of references for ints or the space for the ints themselves? I will be using 64-bit memory addresses for the example.
For example, if I create an int[] numbers = new int[4];
Did java do:
[64-bit array address]
[64-bit address][64-bit address][64-bit address][64-bit address]
The 64-bit address being a pointer to the memory address of the int.
or:
[64-bit array address]
[32-bit int][32-bit int][32-bit int][32-bit int]
using the method of adding 32 bits to the first memory address to address
the remaining elements in the array?
Keep in mind that I am asking about an array of a primitive data type, I fully understand that allocation of memory for an array of objects happens like the first example.
I recently moved a program I was developing from a 32-bit system to a 64-bit system and drastically less objects than the theoretical maximum for the system could be created. If Java does indeed use the first method for allocation of primitive data types, when allocating an array of ints, that array will be one 33% larger moving from a 32-bit system to a 64-bit system, and 66% larger than is really necessary.
- 10-13-2009, 09:41 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
ints in Java are 32 bits no matter the platform.
- 10-13-2009, 10:03 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
r035198x, Thank you for your response, but that is not what I was asking at all. I am aware that ints are 32-bit across platforms. My question was pertaining to how an array of primitive data types are allocated in memory. As an array 64-bit pointers to the primitive data type or as an array of those actual data types in contiguous memory space.
- 10-13-2009, 10:16 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
I thought of a way to figure this out using Instrumentation. The answer is that Java does allocate arrays of primitive types using the second method I suggested.
- 10-13-2009, 10:25 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
If you knew that integers are 32 bits then you would have known the answer.
Especially if you know what happens when you doas opposed to when you doJava Code:int a = 42;
Java Code:Object a = new Object();
- 10-13-2009, 11:37 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 4
- Rep Power
- 0
r035198x, Thank you for your follow up response. I appreciate the promptness of this forum to assist with a problem. r035198x, you have still missed the premise of the question. The question states that I am aware that integers are 32-bits. The question was querying how close Java sticks to the Object Oriented paradigm. In strict object orientation even an array of primitive types would be treated as an array of object pointers pointing to the primitive typed objects. So you will see that knowing that integers are 32-bits does not aid in answering the question. I apologize if my question was unclear in any way. If anyone can think of how I might improve clarity, I will gladly accept advice so that I might avoid confusion in the future. Thanks again for your time.
- 10-14-2009, 12:25 AM #7
an int[] has a 32 bit or 64 bit reference depending on the 32 bit or 64 bit JVM. Each item in that array is a primitive of type int and thus each is 32 bits.
[reference size + other object overhead + 32 bits * length]Last edited by mrmatt1111; 10-14-2009 at 12:27 AM.
My Hobby Project: LegacyClone
- 10-14-2009, 08:02 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Read my last response. It's nothing much to do with arrays but to do with primitive types vs reference types. The variables of primitive types themselves hold the value while variables of reference types do not hold the object but point to the location of the object.
- 10-14-2009, 08:04 AM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Memory Allocation
By kishan in forum New To JavaReplies: 3Last Post: 09-19-2009, 05:47 PM -
Resource management allocation
By jyothi.priyanka in forum New To JavaReplies: 0Last Post: 04-04-2009, 01:25 PM -
how do I increase memory allocated to code cache (Non Heap Memory)
By manibhat in forum Advanced JavaReplies: 2Last Post: 08-21-2008, 07:33 PM -
Out of memory
By mew in forum New To JavaReplies: 1Last Post: 01-20-2008, 08:55 AM -
Memory
By mew in forum CLDC and MIDPReplies: 0Last Post: 12-28-2007, 11:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks