C graphics
C graphics

Reputation: 7458

How memory allocations form in heap when we create a list of arrays in java?

I wonder how the memory allocation forms by JVM when the code below executes. What happens to memory at compile time, what happens at execution time , etc.(would be nice if your answer is with reference to java memory model or possibly something visual) Thank you in advance

PS. This question is not about list of arrays in java

    List<int[]> arList = new ArrayList<>();

    arList.add(new int[]{1,2});
    arList.add(new int[]{1,2,3,4});

    int n = (int)Math.random()*10;
    int [] a = new int[n];
    arList.add(a);

Upvotes: 3

Views: 294

Answers (2)

Gerold Broser
Gerold Broser

Reputation: 14782

Just to add "something visual". That's what I got taught in my JCP preparation class a while ago on a more abstract level. (Well, not exactly that. But ...)

runtime stack
+---+
| a | --------------------------------------+
+---+                                       |
+---+                                       |
| n |                                       |
+---+                                       |
+--------+                                  |
| arList | --+                              |
+--------+   |                              |
             |                              |
             |       runtime heap           |
             v                              |
       +-----------+     +-------+          |
       | ArrayList | --> | 1 | 2 |          |
       +-----------+     +-------+          |
          |    |       +---------------+    |
          |    +-----> | 1 | 2 | 3 | 4 |    |
          |            +---------------+    v
          |                               +---+-...
          +-----------------------------> | 0 | {n times}
                                          +---+-...

I think that's the least common denominator before it becomes implementation-specific.

Upvotes: 0

Hot Licks
Hot Licks

Reputation: 47759

The ArrayList and all 3 int arrays get created in the heap in the (more or less) normal fashion (at least so long as the JITC doesn't do stack allocation -- which it would never do for a non-looping main method).

import java.util.*;
public class IntArrays {
    public static void main(String[] args) {
        List<int[]> arList = new ArrayList<>();

        arList.add(new int[]{1,2});
        arList.add(new int[]{1,2,3,4});

        int n = (int)Math.random()*10;
        int [] a = new int[n];
        arList.add(a);
    }
}

The disassembly:

C:\JavaTools>javap -c IntArrays.class
Compiled from "IntArrays.java"
public class IntArrays {
  public IntArrays();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":
()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/util/ArrayList
       3: dup
       4: invokespecial #3                  // Method java/util/ArrayList."<init
>":()V
       7: astore_1
       8: aload_1
       9: iconst_2
      10: newarray       int  <<< Create the first array
      12: dup
      13: iconst_0
      14: iconst_1
      15: iastore
      16: dup
      17: iconst_1
      18: iconst_2
      19: iastore
      20: invokeinterface #4,  2            // InterfaceMethod java/util/List.ad
d:(Ljava/lang/Object;)Z
      25: pop
      26: aload_1
      27: iconst_4
      28: newarray       int  <<< Create the second array
      30: dup
      31: iconst_0
      32: iconst_1
      33: iastore
      34: dup
      35: iconst_1
      36: iconst_2
      37: iastore
      38: dup
      39: iconst_2
      40: iconst_3
      41: iastore
      42: dup
      43: iconst_3
      44: iconst_4
      45: iastore
      46: invokeinterface #4,  2            // InterfaceMethod java/util/List.ad
d:(Ljava/lang/Object;)Z
      51: pop
      52: invokestatic  #5                  // Method java/lang/Math.random:()D
      55: d2i
      56: bipush        10
      58: imul
      59: istore_2
      60: iload_2
      61: newarray       int  <<< Create the 3rd array
      63: astore_3
      64: aload_1
      65: aload_3
      66: invokeinterface #4,  2            // InterfaceMethod java/util/List.ad
d:(Ljava/lang/Object;)Z
      71: pop
      72: return
}

What's unknown is how many objects are created internal to the ArrayList. In the simplest case there would be one Object array (this is what I see in the copy of JDK 6 source I have), but there's nothing to prevent an implementation from using some sort of linked list were each element is a separate object, or some other scheme.

Upvotes: 1

Related Questions