java 分配的变量引用在哪里,在堆栈中还是在堆中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/873792/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:10:20  来源:igfitidea点击:

Where is allocated variable reference, in stack or in the heap?

javaconcurrencystackheap

提问by iberck

I have a question

我有个问题

What happend when I declare a variable inside a method, for example.

例如,当我在方法中声明一个变量时会发生什么。

void myMethod() {
    Ship myShip = new Ship();
}

Where is allocated myShip reference, in stack or in the heap ?

在哪里分配 myShip 引用,在堆栈中还是在堆中?

I think in stack but I'm confused because I was reading in J2ME Game Programming book "Java classes are instantiated onto the Java heap"

我认为在堆栈中,但我很困惑,因为我正在阅读 J2ME 游戏编程书籍“Java 类被实例化到 Java 堆上”

All java clases ?

所有 java 类?

Thanks in advance

提前致谢

回答by nash

myShipis a reference to a Shipobject, myShipis on the method call stack, which is referred to as "the stack". When a method is called a block of memory is pushed onto the top the stack, that memory block has space for all primitives (int, float, boolean etc) and object references of the method, which includes the method parameters. The heap is where the memory for the actual objects is allocated.

myShip是对一个Ship对象的引用,myShip是在方法调用栈上的,称为“栈”。当一个方法被调用时,一个内存块被推到堆栈的顶部,该内存块有空间用于所有基元(int、float、boolean等)和方法的对象引用,其中包括方法参数。堆是为实际对象分配内存的地方。

So myShipis on the stack and the Shipobject is on the heap.

所以myShip在堆栈上,Ship对象在堆上。

Note each thread has its own stack but share the heap.

请注意,每个线程都有自己的堆栈,但共享堆。

回答by Charlie Martin

Java really does things a bit differently. The referenceis basically on the stack. The memory for the object is allocated in what passes for the heap. However, the implementation of allocable memory isn't quite like the way the heap is implemented in the C/C++ model.

Java 的做法确实有点不同。所述参考是基本上在堆栈中。对象的内存分配给堆。但是,可分配内存的实现与 C/C++ 模型中堆的实现方式不太一样。

When you create a new object like that, it effectively puts the name into the table of references for that scope. That's much like a pointer to an object in C++. When it goes out of scope, that reference is lost; the allocated memory is no longer referenced, and can be garbage-collected.

当您像这样创建一个新对象时,它有效地将名称放入该范围的引用表中。这很像 C++ 中指向对象的指针。当它超出范围时,该引用将丢失;分配的内存不再被引用,并且可以被垃圾收集。

回答by Michael Myers

Currently, all Java objects are allocated on the heap. There is talk that Java 7 might do escape analysis and be able to allocate on the stack, but I don't know if the proposal is finalized yet. Here's the RFE.

目前,所有 Java 对象都在堆上分配。有传言说 Java 7 可能会进行逃逸分析并能够在堆栈上分配,但我不知道该提案是否已最终确定。这是 RFE

Edit:Apparently, it's already in early builds of JDK 7. (The article says it will also be in JDK 6u14, but I can't find confirmation.)

编辑:显然,它已经在 J​​DK 7 的早期版本中。(文章说它也将在 JDK 6u14 中,但我找不到确认。)

回答by Neil Coffey

Notionally, the object goes on "the heap". Then, because it's a method-local reference, the actual reference will be on the stack. By "the" stack, we mean the native thread stack (i.e. the same stack that a local variable in C would be allocated on) in the case of Sun's VM at least, but I don't think that's actually a requirement (the JVM just has to have some abstract notion of "stack frames" that it allocates on each method call, be it from the native stack or not).

从概念上讲,对象继续在“堆”上。然后,因为它是方法局部引用,所以实际引用将在堆栈上。通过“该”堆栈,我们指的是至少在 Sun 的 VM 的情况下的本机线程堆栈(即,将在 C 中分配局部变量的同一堆栈),但我认为这实际上不是一个要求(JVM只需要有一些“堆栈帧”的抽象概念,它会在每次方法调用时分配,无论是否来自本机堆栈)。

But... on modern VMs (with admittedly the possible exception of simpler embedded/mpbile VMs), there's really no such thing as "the" heap. In practice, there are various heap areas. The simplest of these is typically almost like a "mini stack", designed to be quick to allocate for objects that won't hang around for long and can probably be de-allocated almost at once.

但是……在现代虚拟机上(无可否认,更简单的嵌入式/mpbile 虚拟机可能是个例外),实际上没有“堆”这样的东西。在实践中,有各种堆区域。其中最简单的通常几乎就像一个“迷你堆栈”,旨在为不会长时间闲置并且几乎可以立即取消分配的对象快速分配。

As mentioned by another poster, a highly optimised JVM couldin principle allocate object data on the stack and there are definite proposals for this. Although, as also mentioned in one of the references, a criticism of this is that the fast "eden" heap is almost like a stack anyway (just not "the" stack).

正如另一个海报所提到的,高度优化的JVM可以在原则分配堆栈上的对象数据和存在用于此具体的提议。虽然,正如在其中一篇参考文献中提到的,对此的批评是快速的“伊甸园”堆无论如何都几乎像一个堆栈(只是不是“这个”堆栈)。