Java 原语数组是存储在堆栈中还是堆中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2099695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is a Java array of primitives stored in stack or heap?
提问by user241924
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.
这a
是一个原始int
类型的数组。这个数组存储在哪里?它存储在堆上还是堆栈上?这是一个原始类型int
,所有原始类型都不存储在堆上。
回答by GuruKulki
It will be stored on the heap
它将存储在堆上
because array is an object in java.
因为数组是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."
将此代码视为对编译器说:“创建一个包含四个整数的数组对象,并将其分配给名为 的引用变量testScores
。另外,继续将每个int
元素设置为零。谢谢。”
回答by Nikola Gedelovski
In the Java programming language arrays are objects, are dynamically created, and may be assigned to variables of type Object.
在 Java 编程语言中,数组是对象,是动态创建的,并且可以分配给 Object 类型的变量。
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
回答by Esben Skov Pedersen
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.
它是一个原始类型数组,它本身不是原始类型。一个好的经验法则是,当涉及到 new 关键字时,结果将在堆上。
回答by Jon Skeet
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 variableshave their values on the stack, but not all primitive variables are local...
正如 gurukulki 所说,它存储在堆上。但是,您的帖子暗示了一种误解,这可能是由于某些善意的人传播了“原始人总是生活在堆栈中”的神话。这是不真实的。局部变量在堆栈上有它们的值,但并非所有原始变量都是局部的......
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.)
现在,f.value
住在哪里?神话会暗示它在堆栈上 - 但实际上它是新Foo
对象的一部分,并且存在于堆1 上。(请注意,f
它本身的值是一个引用,并且存在于堆栈中。)
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:
从那里,这是阵列的简单步骤。您可以将数组视为许多变量 - 所以new int[3]
有点像具有这种形式的类:
public class ArrayInt3
{
public readonly int length = 3;
public int value0;
public int value1;
public int value2;
}
1In 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 conceptuallyon the heap, if you choose to care.
1事实上,它比这更复杂。堆栈/堆的区别主要是一个实现细节 - 我相信一些 JVM,可能是实验性的,可以判断对象何时永远不会从方法中“逃逸”,并且可能会在堆栈上分配整个对象。但是,如果您选择关心,它在概念上是在堆上。
回答by acheron55
I just wanted to share few tests I ran on this subject.
我只是想分享我在这个主题上运行的一些测试。
Array of size 10 million
1000万大小的数组
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).
如您所见,使用的堆大小增加了 ~80 MB,即 10m * sizeof(double)。
But if we have use Double instead of double
但是如果我们使用 Double 而不是 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.
输出将显示 40MB。我们只有双引用,它们没有被初始化。
Filling it with Double
用 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.
还是 40MB。因为它们都指向同一个 Double 对象。
Initializing with double instead
用 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.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.
由于我们每次都创建新的 Double 对象,因此我们炸毁了堆。这显示 Double 类中的值存储在堆中。