0

Is a static class kept along with it’s static variables in the memory after using it once, or is it instantiated along with every variable every time I use it?

To make it more real lets create an example.
Let’s say I want to make a language dictionary for my system not using singletons.
My static language class with 2 static variables:

package server;  
import java.util.Locale;  
import java.util.ResourceBundle;  

public abstract class Language {  
    private static Locale language = new Locale("en", "GB");  
    public static ResourceBundle dictionary = ResourceBundle.getBundle("dictionary_"+Language.language, Language.language);  

    public static void changeLanguage(Locale language){  
        Language.language = language;  
        Language.dictionary = ResourceBundle.getBundle("dictionary_"+Language.language, Language.language);  
    }  
}  

When I use it in the system to get a tekst value like so:

System.out.println(Language.dictionary.getString("system.name"));  

Will the whole class along with dictionary static variable stay in memory until I use it again, or will it be created again, and again eating my memory every time I do so?

4

1 に答える 1

2

実際には、 と呼ばれるメモリ内の特定の領域に格納されているPermGen静的変数です。この領域には、一度使用した静的変数が保持されます。もう一度使用したい場合は、それらを再作成せずにそのスペースからそれらを取得します。

ただし、このスペースは実行時にいっぱいになる可能性があります。ここではGC、変数の収集と参照の削除が開始されます。この場合、変数が削除されGC、それを再度使用したい場合は、再度作成されます。

PermGenについて詳しく読むことができます

于 2014-03-09T18:35:22.947 に答える