JVM在方法的什么地方存储本地原始变量
Where does the JVM store local primitive variables on a method
一个问题:
其中“1”、“2”存放程序运行时。
我只知道“new Object()”将存储在[Heap]中,“1”和“2”是否都存储在[Java Virtual Machine Stacks]或[Method Area]中
多谢!
这里的代码:
class MyClass {
public void fun() {
int a = 1;//where the "1" stored
Integer b = 2;//where the "2" stored
Object c = new Object();//i just know "new Object()" stored in heap
}
}
在概念层面上,您应该参考 JVM 的规范。在实践中,您可能还会在堆栈而不是堆上看到一些对象作为称为逃逸分析的优化,但这取决于 JVM实施,例如热点或OpenJ9等
您还应该知道,用于将原始整数自动装箱到像 Integer 这样的包装器对象的缓存使每种类型 256 个值的情况进一步复杂化(如果我没记错的话是 -128 到 127)
但是尝试将您的应用程序编译为字节码,然后使用 javap 反汇编它以查看在字节码级别发生了什么
每个局部变量都存储在堆栈中。但是在对象类型的情况下,变量只包含对对象的引用,这就是为什么这些类型也被称为引用类型。
对象总是存储在堆内存中,就是这样heap memory is defined in the first place:
The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.
因为Integer
是一个引用类型,在你的例子中
Integer b = 2;
局部变量b
存储在堆栈内存中,并包含对分配给堆的Integer
对象的引用。 Integer
对象本身包含一个包含值 2
.
的字段
同样适用于
Object c = new Object();
Object
实例存储在堆内存中,变量 c
保存对该对象的引用,存储在堆栈内存中。
在契约中,因为int
是原始类型
int a = 1;
声明了一个变量a
,它将被存储在堆栈内存中,直接包含值1
。
但请注意,这只是一个心智模型。只要行为兼容,实际的实现可以做任何它想做的事。
上面的引用暗示了有关行为的相关差异;它说“... heap 在所有 Java 虚拟机线程之间共享”,这与生活在特定堆栈 space 中的局部变量形成对比线。与 JLS, §17.4.1 比较:
17.4.1. Shared Variables
Memory that can be shared between threads is called shared memory or heap memory.
All instance fields, static
fields, and array elements are stored in heap memory. In this chapter, we use the term variable to refer to both fields and array elements.
Local variables (§14.4), formal method parameters (§8.4.1), and exception handler parameters (§14.20) are never shared between threads and are unaffected by the memory model.
一个问题: 其中“1”、“2”存放程序运行时。 我只知道“new Object()”将存储在[Heap]中,“1”和“2”是否都存储在[Java Virtual Machine Stacks]或[Method Area]中 多谢! 这里的代码:
class MyClass {
public void fun() {
int a = 1;//where the "1" stored
Integer b = 2;//where the "2" stored
Object c = new Object();//i just know "new Object()" stored in heap
}
}
在概念层面上,您应该参考 JVM 的规范。在实践中,您可能还会在堆栈而不是堆上看到一些对象作为称为逃逸分析的优化,但这取决于 JVM实施,例如热点或OpenJ9等
您还应该知道,用于将原始整数自动装箱到像 Integer 这样的包装器对象的缓存使每种类型 256 个值的情况进一步复杂化(如果我没记错的话是 -128 到 127)
但是尝试将您的应用程序编译为字节码,然后使用 javap 反汇编它以查看在字节码级别发生了什么
每个局部变量都存储在堆栈中。但是在对象类型的情况下,变量只包含对对象的引用,这就是为什么这些类型也被称为引用类型。
对象总是存储在堆内存中,就是这样heap memory is defined in the first place:
The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.
因为Integer
是一个引用类型,在你的例子中
Integer b = 2;
局部变量b
存储在堆栈内存中,并包含对分配给堆的Integer
对象的引用。 Integer
对象本身包含一个包含值 2
.
同样适用于
Object c = new Object();
Object
实例存储在堆内存中,变量 c
保存对该对象的引用,存储在堆栈内存中。
在契约中,因为int
是原始类型
int a = 1;
声明了一个变量a
,它将被存储在堆栈内存中,直接包含值1
。
但请注意,这只是一个心智模型。只要行为兼容,实际的实现可以做任何它想做的事。
上面的引用暗示了有关行为的相关差异;它说“... heap 在所有 Java 虚拟机线程之间共享”,这与生活在特定堆栈 space 中的局部变量形成对比线。与 JLS, §17.4.1 比较:
17.4.1. Shared Variables
Memory that can be shared between threads is called shared memory or heap memory.
All instance fields,
static
fields, and array elements are stored in heap memory. In this chapter, we use the term variable to refer to both fields and array elements.Local variables (§14.4), formal method parameters (§8.4.1), and exception handler parameters (§14.20) are never shared between threads and are unaffected by the memory model.