Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Question 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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    ints in Java are 32 bits no matter the platform.

  3. #3
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    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.

  4. #4
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    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.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If you knew that integers are 32 bits then you would have known the answer.
    Especially if you know what happens when you do
    Java Code:
    int a = 42;
    as opposed to when you do
    Java Code:
    Object a = new Object();

  6. #6
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    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.

  7. #7
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    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

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by number5isalive View Post
    ...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. ...
    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.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    And please stop posting stupid messages on my profile page.

Similar Threads

  1. Memory Allocation
    By kishan in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 05:47 PM
  2. Resource management allocation
    By jyothi.priyanka in forum New To Java
    Replies: 0
    Last Post: 04-04-2009, 01:25 PM
  3. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  4. Out of memory
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:55 AM
  5. Memory
    By mew in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-28-2007, 11:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •