Java中存储的静态方法和静态变量在哪里? [英] Where are static methods and static variables stored in Java?

查看:179
本文介绍了Java中存储的静态方法和静态变量在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

class A {
    static int i=0;
    static int j;

   static void method() {
       // static k=0; can't use static for local variables only final is permitted
       // static int L;
    }
}

这些变量将在Java中存储在堆中还是堆栈内存?它们是如何存储的?

Where will these variables be stored in Java, in heap or in stack memory? How are they stored?

推荐答案

静态方法(实际上所有方法)以及静态变量都存储在<$ c中$ c> PermGen 堆的一部分,因为它们是反射数据的一部分(类相关数据,而不是实例相关)。

Static methods (in fact all methods) as well as static variables are stored in the PermGen section of the heap, since they are part of the reflection data (class related data, not instance related).

澄清更新

请注意,只有变量及其技术值(基元或引用)存储在PermGen空间中。

Note that only the variables and their technical values (primitives or references) are stored in PermGen space.

如果您的静态变量是对象的引用,则对象本身存储在堆的正常部分(年轻/老一代或幸存者空间)中。那些对象(除非它们是像类等的内部对象)是存储在PermGen空间中。

If your static variable is a reference to an object that object itself is stored in the normal sections of the heap (young/old generation or survivor space). Those objects (unless they are interal objects like classes etc.) are not stored in PermGen space.

示例:

static int i = 1; //the value 1 is stored in the permgen section
static Object o = new SomeObject(); //the reference(pointer/memory address) is stored in the permgen section, the object itself is not.



关于垃圾收集的一句话:


A word on garbage collection:

依赖 finalize(),因为它无法保证运行。完全取决于JVM决定何时运行垃圾收集器以及收集什么,即使对象可以进行垃圾收集也是如此。

Do not rely on finalize() as it's not guaranteed to run. It is totally up to the JVM to decide when to run the garbage collector and what to collect, even if an object is elligible for garbage collection.

当然你可以设置一个静态变量为null,从而删除对堆上对象的引用,但这并不意味着垃圾收集器收集它(即使没有更多的引用)。

Of course you can set a static variable to null and thus remove the reference to the object on the heap but that doesn't mean the garbage collector will collect it (even if there are no more references).

另外 finalize()只运行一次,所以你必须确保它不会抛出异常或以其他方式阻止对象收集。如果通过某些异常停止终结,则不会再次在同一对象上调用 finalize()

Additionally finalize() is run only once, so you have to make sure it doesn't throw exceptions or otherwise prevent the object to be collected. If you halt finalization through some exception, finalize() won't be invoked on the same object a second time.

最后一个注释:代码,运行时数据等的存储方式取决于所使用的JVM,即HotSpot可能与JRockit不同,这在同一个JVM的版本之间可能会有所不同。以上是基于HotSpot for Java 5和6(基本相同),因为在回答时我会说大多数人都使用这些JVM。由于Java 8中的内存模型发生了重大变化,上面的陈述可能不适用于Java 8 HotSpot - 我没有检查Java 7 HotSpot的变化,所以我猜测以上对于那个版本仍然如此,但我不确定这里。

A final note: how code, runtime data etc. are stored depends on the JVM which is used, i.e. HotSpot might do it differently than JRockit and this might even differ between versions of the same JVM. The above is based on HotSpot for Java 5 and 6 (those are basically the same) since at the time of answering I'd say that most people used those JVMs. Due to major changes in the memory model as of Java 8, the statements above might not be true for Java 8 HotSpot - and I didn't check the changes of Java 7 HotSpot, so I guess the above is still true for that version, but I'm not sure here.

这篇关于Java中存储的静态方法和静态变量在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆