We're writing a large GUI app in Scala with a lot of classes, and we've had to increase the PermGen space to be able to load all classes. The app itself shows a series of screen-based activities, each of which loads its own large set of classes. Only one activity is ever loaded/displayed at the any point in time. After going through a couple of activities, we had an OutOfMemoryError
in the PermGen space.
I understand that the PermGen space is garbage collected just like the rest of the heap, but I'm interested to see if I can reduce the PermGen space needed by having e.g. one ClassLoader
per activity so as to allow class unloading.
So:
- I understand that classes loaded by the system ClassLoader cannot be unloaded, as they will forever be referenced by their class loader. Is that true?
- Provided no more instances of a class loaded by my custom class loader are around, and the class loader can be garbage collected, will its classes be unloaded, freeing PermGen space?
- Are there any caveats regarding (or common mistakes that would prevent) class unloading?