Java JVM 在哪里存储原始变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3698078/
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
Where does the JVM store primitive variables?
提问by JRomio
Where does the Java JVM store primitive variables, and how is the memory used by primitives freed after use?
Java JVM 在哪里存储原始变量,原始变量使用后使用的内存是如何释放的?
I guess it is on the stack?
我猜它在堆栈上?
回答by Jon Skeet
Simplistic answer: it depends on where the variable is declared, not on its type.
简单的答案:它取决于变量的声明位置,而不是它的类型。
Local variables are stored on the stack. Instance and static variables are stored on the heap.
局部变量存储在堆栈中。实例和静态变量存储在堆上。
Don't forget that for reference type variables, the value of a variable is a reference, not the object. (Arrays are reference types too - so if you have an int[]
, the values will be on the heap.)
不要忘记,对于引用类型变量,变量的值是引用,而不是对象。(数组也是引用类型 - 所以如果你有一个int[]
,值将在堆上。)
Now, this is potentially an overly simplistic answer, because a smart VM maybe able to detect if a particular reference type variable refers to an object which can never "escape" the current method. If that's the case, it could potentially inline the whole object on the stack.
现在,这可能是一个过于简单的答案,因为智能 VM可能能够检测特定引用类型变量是否引用了永远无法“逃避”当前方法的对象。如果是这种情况,它可能会内联堆栈上的整个对象。
But conceptuallythis model is accurate. So a variable of type int
which is declared as an instance variable like this:
但从概念上讲,这个模型是准确的。因此int
,声明为实例变量的类型变量如下所示:
class Foo
{
private int value;
...
}
will conceptually live on the heap, as part of the data of any instance of Foo
. It will be freed as part of freeing the instance - it's just 4 bytes within the block of data representing a Foo
instance; it doesn't need separate deallocation.
将概念上存在于堆上,作为 的任何实例的数据的一部分Foo
。它将作为释放实例的一部分被释放——它只是代表Foo
实例的数据块中的 4 个字节;它不需要单独的释放。
回答by siddu
- Class objects, including method code and static fields: heap.
- Objects, including instance fields: heap.
- Local variables and calls to methods: stack..
- 类对象,包括方法代码和静态字段:堆。
- 对象,包括实例字段:堆。
- 局部变量和方法调用:堆栈..
回答by Aniket Thakur
Where a variable is stored depends on whether a variable is a local variableor an instance variable.
变量的存储位置取决于变量是局部变量还是实例变量。
Localvariables are stored on the stack. Instanceand staticvariables are stored on the heap.
局部变量存储在堆栈中。实例和静态变量存储在堆上。
Let me explain this with an example. Lets say we have an instance variable animal of some custom class Animal. Animal animal = new Dog(); Here animal is just a referenceand is located on the stack. The actual object is allocated memory on the heap. This reference animal will point to this object memory allocated on the heap. So if you have 3 reference pointing to the same object.
让我用一个例子来解释这一点。假设我们有一个自定义类 Animal 的实例变量动物。动物动物=新狗();这里的animal 只是一个引用并且位于堆栈上。实际对象是在堆上分配的内存。这个引用动物将指向分配在堆上的这个对象内存。因此,如果您有 3 个引用指向同一个对象。
Animal animal1 = new Dog();
Animal animal2 = new Dog();
Animal animal3 = new Dog();
All three reference will be in stack. When I say reference it is just a pointer pointing to the object on the heap. In terms of memory this reference holds the address(not actually there a bit more abstraction here) of the object on the heap. So 4 bytes on 32 bits and 8 bytes on 64 bits. Only when all the three references are dereferencedi.e they are no longer in scope(or rather no longer pointing to the original object) then only garbage collector is free to deallocate the memory allocated to the object on the heap.
所有三个引用都将在堆栈中。当我说引用时,它只是一个指向堆上对象的指针。在内存方面,这个引用保存了堆上对象的地址(实际上这里没有更多的抽象)。所以32 位上有 4 个字节,64 位上有 8 个字节。只有当所有三个引用都被取消引用时,即它们不再在范围内(或者不再指向原始对象),只有垃圾收集器才能释放分配给堆上对象的内存。
There is a slight variation when we store primitive types or String literals.Unless you explicitly create their object using new() operator they are created and stored in permGenarea of Heap. So both references firstString and secondString in
当我们存储原始类型或字符串字面量时,会有一些细微的变化。除非您使用 new() 运算符显式创建它们的对象,否则它们将被创建并存储在Heap 的permGen区域。所以两者都引用了 firstString 和 secondString
String firstString = "Stack";
String secondString = "Stack";
will point to the same object in the String pool. It would point to different objects when we create them using new().
将指向字符串池中的同一个对象。当我们使用 new() 创建它们时,它会指向不同的对象。