简体   繁体   中英

How to know if Static Block Initialization has been run?

I am trying to get rid of some memory leaks. I'd like to reset all the static variables of all the classes (not only mine) from a class loader. There is a classes attribute that lists all the classes known by the ClassLoader.

So I just want to loop over it and with reflection set the static variables to null.

The problem is that all those classes have not necessarily been initialized (the Static Block Initialization did not run). As the purpose is to reset the values and then unload the classes, there is no point initializing this classes. Moreover, when I reset a class ROOT that is used in the SBI of another class CHILD, running the SBI of CHILD can lead to unexpected behavior...

So the question is: Is there a way to know if the SBI has been run by the JVM or not.


Note:

to anybody proposing to use the findLoadedClass of the ClassLoader, there is in the specification this important sentence: *In this post, there is an important note: "loaded" doesn't mean "initialized". initialization only happens at precise moments defined by JLS3 $12.4.1 *

static block was initialized if class was loaded on JVM.

Is class loaded on JVM you can detect with ClassLoader

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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