0

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 *

poussma
  • 7,033
  • 3
  • 43
  • 68
  • 1
    http://stackoverflow.com/questions/3678579/how-to-check-whether-a-class-is-initialized I think that is what youre searching for – LionC May 02 '13 at 13:17
  • How do you know that static blocks are the cause of the memory links? Have you profiled extensively? Unless you know for sure the cause, it's a bad idea to try to optimize prematurely. – Platinum Azure May 02 '13 at 13:18
  • @LionC see the edit of my post – poussma May 02 '13 at 13:39
  • @PlatinumAzure yes. BTW, most of the memory leaks are due to static variables keeping a reference of a classe loader or whatever... For sure resetting values manually through reflection is not a good practice, but in this case, this is what I want to do – poussma May 02 '13 at 13:42

1 Answers1

0

static block was initialized if class was loaded on JVM.

Is class loaded on JVM you can detect with ClassLoader

Ilya
  • 29,135
  • 19
  • 110
  • 158
  • 1
    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*** – poussma May 02 '13 at 13:36