简体   繁体   English

Java:静态字段存在于内存中的哪个位置?

[英]Java: where do static fields live within the memory?

If we store objects in static fields of an object, how does the JVM allocate the memory for it? 如果我们将对象存储在对象的静态字段中,JVM如何为它分配内存? Does it live within "implicit"(not sure if I am using the right word) class object? 它是否存在于“隐含”(不确定我是否使用正确的单词)类对象中? How are static fields different from object fields? 静态字段与对象字段有何不同?

Static fields are class variables, and are shared amongst all instances of that class. 静态字段是类变量,并在该类的所有实例之间共享。 Instance variables (or object fields as I think you are referring to them as) belong to individual instances of a class and are not shared. 实例变量(或我认为你称之为的对象字段)属于类的各个实例,不共享。

As for where they are stored in memory is going to based on the implementation of the JVM and there is no reason two different JVMs would be required to store them in the same place by specification (to the best of my knowledge at least - should insert appropriate spec sheet link here). 至于它们存储在内存中的位置将基于JVM的实现,并且没有理由需要两个不同的JVM来按规范将它们存储在同一个地方(据我所知至少 - 应该插入适当的规格表链接在这里)。

As Nick's answer says, there is no specific "physical" location stipulated by the language specification, but in terms of a logical mental model that you can reason about, it may help you to think of static fields as being attached to the class object ( Foo.class ) of the class those fields belong to. 正如Nick的回答所说,语言规范没有规定具体的“物理”位置,但就你可以推理的逻辑心理模型而言,它可能会帮助你将静态字段视为附加到类对象(这些字段所属的类的Foo.class )。

As an aside, the class object is used in other ways (that are stipulated by the language spec) when dealing with static entities: for example, when calling a synchronized static method, the lock is held on the class object of the class that method belongs to. 例如,调用时:作为题外话,类对象被以其他方式与静态实体处理时(即语言规范中规定)中使用synchronized静态方法,锁被保持在类方法的类对象上属于。

Static fields are part of the class; 静态字段是类的一部分; supposedly, they disappear when the class is unloaded. 据说,当班级被卸下时,它们会消失。 It makes sense to imagine them as being somewhere close to the Class instance for the class. 将它们想象成靠近Class实例是有意义的。 Details on how memory is laid out are beyond the reach of the Java application; 有关内存如何布局的详细信息超出了Java应用程序的范围; as a corollary, the JVM specification mandates nothing specific on that subject. 作为必然结果,JVM规范没有强制要求该主题。 Even the "disappearance" of the fields when the class is unloaded cannot be observed directly, but only through GC action, assuming a well-placed finalize() method. 即使是卸载类时字段的“消失”也无法直接观察到,只能通过GC操作,假设有一个良好放置的finalize()方法。

Static fields are stored within the Class object, which is kept in PemGen space. 静态字段存储在Class对象中,该对象保存在PemGen空间中。 This is part of heap memory. 这是堆内存的一部分。

As NickLarsen said, I don't think there is any JVM specification how exactly static fields are stored. 正如NickLarsen所说,我认为没有任何JVM规范如何存储静态字段。 Compile defined constants (static final) will replace most likely the expression during compile time. 编译定义的常量(static final)将在编译期间替换最可能的表达式。 For variable static fields there will be only two options: The heap or (if existing) the data segment of the JVM. 对于变量静态字段,只有两个选项:堆或(如果存在)JVM的数据段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM