JVM在哪里存储原始变量? [英] Where does the JVM store primitive variables?

查看:95
本文介绍了JVM在哪里存储原始变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java JVM在哪里存储原始变量,以及原语在使用后释放的内存是如何释放的?

Where does the Java JVM store primitive variables, and how is the memory used by primitives freed after use?

我猜它是在堆栈上?

推荐答案

简单回答:它取决于声明变量的位置,而不是它的类型。

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.

不要忘记,对于引用类型变量,变量的值是引用,而不是对象。 (数组也是引用类型 - 所以如果你有一个 int [] ,那么这些值将在堆上。)

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.)

现在,这可能是一个过于简单化的答案,因为智能VM 可以能够检测特定引用类型变量是否引用永远不会逃避当前方法的对象。如果是这种情况,它可能会内联堆栈中的整个对象。

Now, this is potentially an overly simplistic answer, because a smart VM may be 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.

概念上这个模型是准确的。所以类型 int 的变量被声明为这样的实例变量:

But conceptually this model is accurate. So a variable of type int which is declared as an instance variable like this:

class Foo
{
    private int value;
    ...
}

将在概念上生活在堆上,作为一部分 Foo 的任何实例的数据。它将作为释放实例的一部分释放 - 它只是表示 Foo 实例的数据块中的4个字节;它不需要单独释放。

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.

这篇关于JVM在哪里存储原始变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆