开发者

How static variable treated by gc

开发者 https://www.devze.com 2023-01-02 07:52 出处:网络
How are static variab开发者_StackOverflowle treated by the garbage collector and where are they allocated memory? on the heap or stack (as member variables)

How are static variab开发者_StackOverflowle 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:

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. Classes and interfaces loaded by the bootstrap loader may not be unloaded.

Static fields are allocated on the heap.

0

精彩评论

暂无评论...
验证码 换一张
取 消