Java JVM 中存储的对象的实例变量在哪里?

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

Where are instance variables of an Object stored in the JVM?

javajvmheap-memoryjvm-hotspot

提问by saurabh goyal

Is an instance variable of an object in Java stored on the stack or method area of the JVM?

Java中对象的实例变量是否存储在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?

如果它存储在方法区中,实例变量与静态变量存储有何不同?

回答by Matej ?pilár

Stackand heapare the memories allocated by the OS to the JVM that runs in the system.Stackis a memory place where the methods and the local variables are stored. (variable references either primitiveor objectreferences are also stored in the stack). Heapis a memory place where the objects and its instance variable are stored.

堆栈是操作系统分配给系统中运行的 JVM 的内存。堆栈是存储方法和局部变量的内存位置。(变量引用无论是原始引用还是对象引用也存储在堆栈中)。是存储对象及其实例变量的内存位置。

So to sum it up:

所以总结一下:

  • Class objects, including method code and static fields: heap.
  • Objects, including instance fields: heap.
  • Local variables and calls to methods: stack
  • 类对象,包括方法代码和静态字段:堆。
  • 对象,包括实例字段:堆。
  • 局部变量和方法调用:堆栈

Also, do we have different instance variable for multiple threads?

另外,我们是否有多个线程的不同实例变量?

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.

每个线程都有一个程序计数器 (PC) 和一个 java 堆栈。PC 将使用 java 堆栈来存储中间值、动态链接、方法的返回值和调度异常。这用于代替寄存器。

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 fieldsare stored in heap. On the other hand, local variablesare stored in stack.

如您所见,静态字段存储在堆中。另一方面,局部变量存储在堆栈中。

//EDIT

//编辑

According to the comments of Bruno Reisand Peter Lawrey, you should also read about Escape analysis

根据Bruno ReisPeter Lawrey的评论,您还应该阅读Escape 分析

  1. Wikipedia
  2. Virtual Machine Performance Enhancements,Escape Analysis
  1. 维基百科
  2. 虚拟机性能增强、逃逸分析

回答by Jayanth

To be precise,

准确地说,

  • Instance variables will be stored on the heap.
  • local variables on the stack(in case of variable not a primitive[reference variable] reference variables live on stack
    and the object on heap ). Only method invocation and partial results will be stored in stack not the method itself.
  • Static variables and Methods(including static and Non Static) on the Method Area.
  • 实例变量将存储在堆上。
  • 堆栈上的局部变量(如果变量不是原始[引用变量] 引用变量存在于堆栈
    和堆上的对象)。只有方法调用和部分结果将存储在堆栈中,而不是方法本身。
  • 方法区上的静态变量和方法(包括静态和非静态)。

Reference: Head First Java

参考:Head First Java