4

My program uses ASM library to generate an adhoc class for efficient computation. Each call generates a new class.

The class loader instance which defines the adhoc class and all instances of the adhoc class will be unreachable in a short time. Will the adhoc class code be freed after this?

Sherwood Wang
  • 685
  • 9
  • 19
  • 1
    See [this](http://stackoverflow.com/questions/2344964/when-and-how-is-a-java-classloader-marked-for-garbage-collection) answer it's really helpful – karim mohsen Aug 11 '15 at 09:45
  • 1
    -XX:CMSClassUnloadingEnabled might be useful to remove classes no longer in use: http://stackoverflow.com/a/3334954/458157 – gouessej Aug 11 '15 at 09:58

2 Answers2

3

Yes, after the classloader is eligible for garbage collection, then the classes it has loaded will be eligible for garbage collection as well (provided no other references to the classes).

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

Depends what you mean "after this". You can't force the garbage collector to run, the environment itself will decide whether or not to run, even if you "explicitly call" it.

Actually, calling the gc to run, is merely 'requesting' it to run.

So: will a no-longer-referenced variable be cleaned up? Yes. When will it happen? This we can't say for sure. Basically, the answer to this is: "when the gc decides to clean it up".

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • I agree with this answer. Even running the finalization doesn't force the garbage collection to run, it's just an hint, it's free to ignore it. – gouessej Aug 11 '15 at 10:00