Where are instance variables of an Object stored in the JVM?(对象的实例变量存储在 JVM 中的什么位置?)
问题描述
Java中对象的实例变量是否存储在JVM的堆栈或方法区?
Is an instance variable of an object in Java stored on the stack or method area of the JVM?
另外,我们是否有多个线程的不同实例变量?
Also, do we have different instance variable for multiple threads?
如果存储在方法区,实例变量与静态变量存储有何不同?
If it is stored in method area how is instance variable different from static variable storage?
推荐答案
Stack和heap是操作系统分配给JVM的内存,运行在system.堆栈是存储方法和局部变量的内存位置.(变量引用 primitive 或 object 引用也存储在堆栈中).堆是存储对象及其实例变量的内存位置.
Stack and heap are the memories allocated by the OS to the JVM that runs in the system.Stack is a memory place where the methods and the local variables are stored. (variable references either primitive or object references are also stored in the stack). Heap is a memory place where the objects and its instance variable are stored.
总结一下:
- 类对象,包括方法代码和静态字段:堆.
- 对象,包括实例字段:堆.
- 局部变量和方法调用:堆栈
另外,我们是否有多个线程的不同实例变量?
Also, do we have different instance variable for multiple threads?
每个线程都有一个程序计数器 (PC) 和一个 java 堆栈.PC 将使用 java 堆栈来存储中间值、动态链接、方法的返回值和调度异常.这用于代替寄存器.
Every thread will have a program counter (PC) and a java stack. PC will use the java stack to store the intermediate values, dynamic linking, return values for methods and dispatch exceptions. This is used in the place of registers.
另外关于线程的更多信息,你真的应该阅读这个主题 Where线程对象是创建的吗?堆栈还是堆?.
Also for more about thread, you really should read this topic Where is Thread Object created? Stack or Heap?.
如果它存储在方法区域中,实例变量与静态变量存储?
If it is stored in method area how is instance variable different from static variable storage?
如上所示,静态字段存储在堆中.另一方面,局部变量存储在堆栈中.
As you can see above static fields are stored in heap. On the other hand, local variables are stored in stack.
//编辑
根据 Bruno Reis 和 Peter Lawrey,您还应该阅读逃逸分析
According to the comments of Bruno Reis and Peter Lawrey, you should also read about Escape analysis
- 维基百科
- 虚拟机性能增强,Escape分析
这篇关于对象的实例变量存储在 JVM 中的什么位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对象的实例变量存储在 JVM 中的什么位置?
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01