Java 类区域和堆的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19340013/
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
Difference between class area and heap
提问by trailblazer
The JVM allocates these areas in memory: Class(Method) Area, Heap, Stack, Program counter Register, Native method stack.
JVM 在内存中分配这些区域:类(方法)区、堆、堆栈、程序计数器寄存器、本地方法堆栈。
I know that heap is used to store objects and stack is used to store local variables and partial results. According to definition, Class(Method) Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods. But I really don't understand the difference.
我知道堆用于存储对象,堆栈用于存储局部变量和部分结果。根据定义,Class(Method) Area 存储每个类的结构,例如运行时常量池、字段和方法数据、方法代码。但我真的不明白其中的区别。
Can anyone please explain the difference between Class area and Heap?
谁能解释一下类区域和堆之间的区别?
采纳答案by Peter Lawrey
Java 6 stores all the constant pool and Class information in the Perm Gen
Java 6 将所有常量池和类信息存储在 Perm Gen 中
Java 7 only stores the class information in the Perm Gen. The String literal pool is on the heap.
Java 7 只在 Perm Gen 中存储类信息。字符串文字池在堆上。
Java 8 has no Perm Gen. The literal pools and class information are on the heap.
Java 8 没有 Perm Gen。文字池和类信息在堆上。
You have explained the difference. Class structures like methods are stored in perm gen. The data in each instance is stored in the heap.
你已经解释了区别。像方法这样的类结构存储在 perm gen 中。每个实例中的数据都存储在堆中。
They were separated as these types of data have very different lifecycles e.g. Objects are typically short lived and classes are typically very long lived.
它们是分开的,因为这些类型的数据具有非常不同的生命周期,例如对象通常是短暂的,而类通常是非常长的。
AFAIK They are removing it because too many developers found it confusing.
AFAIK 他们正在删除它,因为太多开发人员发现它令人困惑。
回答by Luke Briggs
The class (method) area stores code- that's the code of your program. The heap stores object instances. For example:
类(方法)区域存储代码- 那是您程序的代码。堆存储对象实例。例如:
public void MakeSomeFruit(){
Fruit myFruit=new Fruit();
}
- The MakeSomeFruit code is stored in the class area.
- When executed, the actual Fruit instance it creates is stored in the Heap.
- When executed, the myFruit referencevariable is stored on the stack. That's just a number which points at the location of the instance in memory - an address.
- MakeSomeFruit 代码存储在类区域中。
- 执行时,它创建的实际 Fruit 实例存储在堆中。
- 执行时,myFruit引用变量存储在堆栈中。这只是一个指向实例在内存中的位置的数字——一个地址。