Conserving PermGen Space with Multiple Class Loaders

We are writing a large graphical application in Scala with a large number of classes, and we had to increase the PermGen space in order to be able to load all classes. The application itself shows a series of on-screen actions, each of which loads its own large set of classes. Only one action is loaded / displayed at any time. After a couple of steps, we had an OutOfMemoryError in PermGen space.

I understand that PermGen space is garbage collected in the same way as the rest of the heap , but I'm interested in knowing if I can reduce the PermGen space needed for having, for example, one ClassLoader per activity, to allow class unloading.

So:

  • I understand that classes loaded by the ClassLoader system cannot be unloaded, as their class loader will always refer to them. It's true?
  • If there are no more instances of the class loaded by my class loader, and the class loader can be garbage collected, will its classes be unloaded, freeing up PermGen space?
  • Are there any warnings regarding (or common errors that may interfere) class unloading?
+6
source share
1 answer

... if I can reduce the PermGen space needed, for example, one ClassLoader per activity to allow class unloading.

Yes, the only way classes can be unloaded is if the Classloader loader is used, it is garbage collection. This means that references to each individual class and to the class loader must be zero.

How big is your PermGen? You can leave just by bumping into PermGen:

 -XX:MaxPermGen=256m 

on the command line. It is not uncommon to set it to 512 m. If you want a truly reliable solution, you will need to go the way of using a custom class loader for "activity". To help with debugging, add the following explanatory argument to your command line:

 -XX:+TraceClassLoading 

This will print the classes as they are loaded into the JVM on the command line.

+6
source

Source: https://habr.com/ru/post/899314/


All Articles