Java 静态变量的实际内存位置是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6569557/
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
What is the actual memory place for static variables?
提问by Reuben
Static variable is allocated for the entire duration of program's execution, so neither stack nor heap are convenient for it. Then where is it? There should be some place where it is loaded ?
静态变量在程序执行的整个持续时间内分配,因此堆栈和堆都不方便。那么它在哪里呢?应该有什么地方加载它?
采纳答案by Peter Lawrey
Static fields are initialised when a class is loaded and and are discarded when the classloader for that class is unloaded. They can be cleaned up, even duplicated in another class loader.
静态字段在加载类时初始化,并在卸载该类的类加载器时丢弃。它们可以被清理,甚至可以在另一个类加载器中复制。
For applications like those this use OSGi, static variables don't live for the life of the application can be reloaded many times.
对于像这样使用 OSGi 的应用程序,静态变量在应用程序的生命周期内不会存在,可以多次重新加载。
How this is implement may be JVM dependant but the Sun/Oracle JVM creates an "object" to hold the static fields for a class. This object is accessible via the Unsafe class which can also be used to examine this "objects" fields.
这是如何实现的可能取决于 JVM,但 Sun/Oracle JVM 创建一个“对象”来保存类的静态字段。该对象可通过 Unsafe 类访问,该类也可用于检查此“对象”字段。
回答by Rupok
From http://www.daniweb.com/software-development/java/threads/34695:
从http://www.daniweb.com/software-development/java/threads/34695:
Static variable's memory is allocated at the start of the program, in regular memory, instead of the stack (memory set aside specifically for the program). the advantage of this is that it makes your variable or procedure totally constant, and you can't accidentally change the value. the disadvantage of this is that the memory is not deallocated until the program is terminated. I have never heard anything that static values take any more memory than if they are declared regularly, but thier memory use is constant throught.
静态变量的内存在程序开始时分配,在常规内存中,而不是堆栈(专门为程序留出的内存)。这样做的好处是它使您的变量或过程完全不变,并且您不会意外更改该值。这样做的缺点是在程序终止之前不会释放内存。我从来没有听说过静态值比定期声明占用更多的内存,但是它们的内存使用是始终不变的。
回答by Stephen C
Static variable is allocated for the entire duration of program's execution, so neither stack nor heap are convenient for it.
静态变量在程序执行的整个持续时间内分配,因此堆栈和堆都不方便。
In fact, static frames (i.e. the frames that hold the static variables) ARE allocated from the heap.
事实上,静态帧(即保存静态变量的帧)是从堆中分配的。
And they don't necessarily exist for the duration of a program's execution. For instance, static frames for classes that are dynamically loaded can be garbage collected if the parent classloader, all classes and all instances becomes unreachable.
而且它们不一定在程序执行期间存在。例如,如果父类加载器、所有类和所有实例都无法访问,则动态加载的类的静态帧可能会被垃圾收集。
回答by Sourav
We have 3 segments in our memory:
我们的记忆中有 3 个片段:
Stack Segment — contains local variables and Reference variables (variables that hold the address of an object in the heap).
Heap Segment — contains all created objects in runtime, objects only plus their object attributes (instance variables).
Code Segment — the segment where the actual compiled Java bytecodes resides when loaded. Static members (variables or methods) are called class members, meaning they reside where the class (bytecode) resides, which is in the Code Segment.
堆栈段——包含局部变量和引用变量(保存堆中对象地址的变量)。
堆段 - 包含运行时创建的所有对象,仅对象加上它们的对象属性(实例变量)。
代码段——加载时实际编译的 Java 字节码所在的段。静态成员(变量或方法)称为类成员,这意味着它们驻留在类(字节码)所在的位置,即代码段中。
回答by hi.nitish
Out of five memory areas that JVM uses, the static fields are allocated memory in Class Area(part of PremGen)when the class is loaded by the Application class loader during prepare and loading phase. If the field is primitive, the value is stored in the class area and if it is of Object type (new operator used), it is stored in heap but the reference is given to the assigned static field variable in class area. When the class is unloaded, the memory for that static field is also available to be garbage collected by GC.
在 JVM 使用的五个内存区域中,静态字段在准备和加载阶段由应用程序类加载器加载类时在类区域(PremGen 的一部分)中分配内存。如果字段是原始字段,则将值存储在类区域中,如果是对象类型(使用新运算符),则将其存储在堆中,但将引用提供给类区域中分配的静态字段变量。当类被卸载时,该静态字段的内存也可以被 GC 垃圾回收。
If the field is final as well, that is, static final, it is kept in constant pool under class area.
如果该字段也是final的,即静态final,则将其保存在类区域下的常量池中。
回答by Atul Verma
The static variables are provided the memory in the the same memory segment where the code is stored i.e. Class Area. It is independent of the stack or heap segment of memory. It remains accessible during the complete duration of the program.
静态变量提供在存储代码的同一内存段中的内存,即类区。它独立于内存的堆栈或堆段。在整个计划期间,它仍然可以访问。