Where are static methods and static variables stored in Java? Ask Question

Where are static methods and static variables stored in Java? Ask Question

For example:

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;
    }
}

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

ベストアンサー1

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). As of Java 8 PermGen has been replaced by MetaSpace and as per JEP 122 it only holds meta-data while static fields are stored in the heap.

Note that this mostly applies to Oracle's Hotspot JVM and others that are based on it. However, not every JVM has PermGen or Metaspace like Eclipse OpenJ9.

Update for clarification:

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

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 internal objects like classes etc.) are not stored in PermGen space.

Example:

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:

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 eligible for garbage collection.

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).

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.

A final note: コードやランタイムデータなどの保存方法は、使用するJVMによって異なります。つまり、HotSpotはJRockitとは異なる方法で保存する可能性があり、同じJVMのバージョン間でも異なる場合があります。上記は、Java 5と6のHotSpotに基づいています(これらは基本的に同じです)。回答時点では、ほとんどの人がこれらのJVMを使用していたと思います。Java 8のメモリモデルに大きな変更があったため、上記の記述はJava 8 HotSpotには当てはまらない可能性があります。また、Java 7 HotSpotの変更については確認していないため、推測上記はそのバージョンでも当てはまりますが、ここではわかりません。

おすすめ記事