2

I'm using a custom classloader to load some java classes. I need to execute some methods from these loaded classes in a loop. For each loop iteration I need a fresh initialization of all the classes (all static fields). I have measured that the execution time is three times slower if I use a new classloader for each iteration than the execution time when not using a fresh classloader in each iteration.

Can I reset the loaded classes to their initial state without loading them with a new classloader?

Or is there a way to speed up recurring loading of the same classes in different classloaders?

tangens
  • 39,095
  • 19
  • 120
  • 139

2 Answers2

2

When you load the class with a new classloader, the JMV will almost certainly have to re-jit the byte code. Until it does, the first uses of the newly loaded classes will be slower.

I assume these classes are library code which you cannot modify? Because the fact that you're having to use the classes in this way suggests flawed design to me.

ulmangt
  • 5,343
  • 3
  • 23
  • 36
  • It's a security feature. The classes are uploaded by the user and executed inside the server system. – tangens Apr 22 '12 at 06:12
0

Just from the top of my head: can you take a snapshot of the initial state of the class using reflection and then restoring it?

slvr
  • 56
  • 3