简体   繁体   English

如何用gc处理静态变量

[英]How static variable treated by gc

How are static variable treated by the garbage collector and where are they allocated memory? 垃圾收集器如何处理静态变量以及它们在哪里分配内存? on the heap or stack (as member variables) Please clarify this for me. 在堆或堆栈上(作为成员变量)请为我澄清这一点。

Static variables are only eligible for garbage collection when the class itself is garbage collected -- and classes are only eligible for garbage collection if the classloader which loaded them is garbage collected. 当类本身被垃圾收集时,静态变量仅适用于垃圾收集 - 如果加载它们的类加载器是垃圾收集的,那么类只适用于垃圾收集。

See JLS § 12.7 : JLS§12.7

A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector as discussed in §12.6. 当且仅当其定义的类加载器可以由垃圾收集器回收时,可以卸载类或接口,如第12.6节中所述。 Classes and interfaces loaded by the bootstrap loader may not be unloaded. 可能无法卸载由引导加载程序加载的类和接口。

Static fields are allocated on the heap. 静态字段在堆上分配。

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

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