97

I have an array declaration like this:

int a[];

Here a is an array of primitive int type. Where is this array stored? Is it stored on heap or stack? This is a primitve type int, all primitive types are not stored on heap.

trincot
  • 317,000
  • 35
  • 244
  • 286
user241924
  • 4,340
  • 7
  • 27
  • 25
  • 42
    That is not an array. It is a reference to an array. The reference itself could be stored on the heap if it is a member of a class or object, or on the stack if it is a local variable in a method. And primitive types can be stored on the heap if they are members of a class or object. – UncleO Jan 20 '10 at 07:39

6 Answers6

162

As gurukulki said, it's stored on the heap. However, your post suggested a misunderstanding probably due to some well-intentioned person propagating the myth that "primitives always live on the stack". This is untrue. Local variables have their values on the stack, but not all primitive variables are local...

For example, consider this:

public class Foo
{
    int value;
}
...

public void someOtherMethod()
{
    Foo f = new Foo();
    ...
}

Now, where does f.value live? The myth would suggest it's on the stack - but actually it's part of the new Foo object, and lives on the heap1. (Note that the value of f itself is a reference, and lives on the stack.)

From there, it's an easy step to arrays. You can think of an array as just being a lot of variables - so new int[3] is a bit like having a class of this form:

public class ArrayInt3
{
    public readonly int length = 3;
    public int value0;
    public int value1;
    public int value2;
}

1 In fact, it's more complicated than this. The stack/heap distinction is mostly an implementation detail - I believe some JVMs, possibly experimental ones, can tell when an object never "escapes" from a method, and may allocate the whole object on the stack. However, it's conceptually on the heap, if you choose to care.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    About the "escape analyisis" in Java: http://blog.juma.me.uk/2008/12/17/objects-with-no-allocation-overhead/ It says that it is present since the early access release of JDK 6 Update 14, and enabled by default since JDK 6 Update 23. – Guido Jun 14 '11 at 20:38
  • does it change anything if array is public static final? Shouldn't it be part of the constant pool then? – Malachiasz Feb 28 '14 at 10:13
  • @Malachiasz: Nope. An array is never a constant. – Jon Skeet Feb 28 '14 at 10:18
  • @JonSkeet: In all versions of the Java library I'm aware of, every `String` is backed by a `char[]`. I believe literal strings are stored in the public constant pool; for GC optimization, that would imply that the backing arrays should be stored likewise (otherwise the constant pool would have to be scanned during any GC cycle where the backing array would otherwise be eligible for collection). – supercat Jun 08 '14 at 20:25
  • @supercat: Yes, that would make sense. But any array you declare *yourself* never ends up as part of the constant pool. – Jon Skeet Jun 09 '14 at 05:45
43

It will be stored on the heap

because array is an object in java.

EDIT : if you have

int [] testScores; 
testScores = new int[4];

Think of this code as saying to the compiler, "Create an array object that will hold four ints, and assign it to the reference variable named testScores. Also, go ahead and set each int element to zero. Thanks."

muneeb_ahmed
  • 364
  • 5
  • 16
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • 5
    And the reference variable named testScores(pointing to the array on the heap) will be on the stack. – Zaki Feb 03 '10 at 08:13
  • 12
    The code only says "Thanks" if you supply the `-g` option to the compiler. Otherwise it will be optimized away. – mob Feb 18 '10 at 17:14
  • 1
    @mob You are making an assumption that this is the only code. It is probably better to assume that these two lines are part of a larger program which actually uses the array. – Code-Apprentice Sep 24 '14 at 19:48
27

It is an array of primitive types which in itself is not primitive. A good rule of thumb is when the new keyword is involved the result will be on the heap.

Esben Skov Pedersen
  • 4,437
  • 2
  • 32
  • 46
23

I just wanted to share few tests I ran on this subject.

Array of size 10 million

public static void main(String[] args) {
    memInfo();
    double a[] = new double[10000000];
    memInfo();
}

Output:

------------------------
max mem = 130.0 MB
total mem = 85.0 MB
free mem = 83.6 MB
used mem = 1.4 MB
------------------------
------------------------
max mem = 130.0 MB
total mem = 130.0 MB
free mem = 48.9 MB
used mem = 81.1 MB
------------------------

As you see used heap size is increased by ~80 MB, which is 10m * sizeof(double).

But if we have use Double instead of double

public static void main(String[] args) {
    memInfo();
    Double a[] = new Double[10000000];
    memInfo();
}

Output will show 40MB. We only have Double references, they are not initialized.

Filling it with Double

public static void main(String[] args) {
    memInfo();
    Double a[] = new Double[10000000];      
    Double qq = 3.1d;
    for (int i = 0; i < a.length; i++) {
        a[i] = qq;
    }
    memInfo();
}

Still 40MB. Because they all point to same Double object.

Initializing with double instead

public static void main(String[] args) {
    memInfo();
    Double a[] = new Double[10000000];
    Double qq = 3.1d;
    for (int i = 0; i < a.length; i++) {
        a[i] = qq.doubleValue();
    }
    memInfo();
}

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Line

a[i] = qq.doubleValue();

is equivalent to

a[i] = Double.valueOf(qq.doubleValue());

which is equivalent to

a[i] = new Double(qq.doubleValue());

Since we create new Double objects each time, we blow out the heap. This shows values inside the Double class are stored in heap.

acheron55
  • 5,429
  • 2
  • 23
  • 22
3

In the Java programming language arrays are objects, are dynamically created, and may be assigned to variables of type Object.

http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

Nikola Gedelovski
  • 1,000
  • 6
  • 12
0

I guess there are two ways of looking at it, when we do something like this

    int arr[] = new int[4];
    for( int i=0;i<=3;i++ ){
        arr[i] = i*10;
    }

than I think the array is stored in heap and only the reference ariable "arr" is stored in stack. But when we do something like this int arr[] = {10,20,30,40,50}; then everything is stored in stack