简体   繁体   English

使用多个类加载器保存PermGen空间

[英]Saving PermGen space with several classloaders

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. 我们在Scala中编写了一个包含大量类的大型GUI应用程序,我们必须增加PermGen空间才能加载所有类。 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. 经过几个活动后,我们在PermGen空间中出现了OutOfMemoryError

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 eg one ClassLoader per activity so as to allow class unloading. 我理解PermGen空间是垃圾收集的,就像堆的其余部分一样 ,但是我很想知道是否可以减少每个活动一个ClassLoader所需的PermGen空间,以便允许类卸载。

So: 所以:

  1. I understand that classes loaded by the system ClassLoader cannot be unloaded, as they will forever be referenced by their class loader. 我知道系统ClassLoader加载的类无法卸载,因为它们将永远被类加载器引用。 Is that true? 真的吗?
  2. 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? 如果我的自定义类加载器不再加载类的实例,并且类加载器可以被垃圾收集,它的类是否会被卸载,释放PermGen空间?
  3. Are there any caveats regarding (or common mistakes that would prevent) class unloading? 是否有任何关于(或常见错误会阻止)类卸载的警告?

...if I can reduce the PermGen space needed by having eg one ClassLoader per activity so as to allow class unloading. ...如果我可以减少每个活动一个ClassLoader所需的PermGen空间,以便允许类卸载。

Yes, the only way Classes are eligible to be unloaded is if the Classloader used is garbage collected. 是的,如果使用的类加载器是垃圾收集的,则可以卸载类的唯一方法。 This means that the references to every single class and to the classloader itself need to be zero. 这意味着对每个类和类加载器本身的引用必须为零。

How big is your PermGen? 你的PermGen有多大? You may get away with just bumping PermGen with : 你可以通过以下方式与PermGen碰撞:

-XX:MaxPermGen=256m

on your command line. 在你的命令行上。 It's not uncommon to set it to 512m. 将其设置为512米并不罕见。 If you want a truly robust solution, you will need to go the route of using a custom class loader per 'activity'. 如果您想要一个真正强大的解决方案,您将需要按照“活动”使用自定义类加载器的路线。 To help with debugging, add the following self explanatory argument to your command line as well: 为了帮助调试,还要在命令行中添加以下自解释参数:

-XX:+TraceClassLoading

This will print out classes as they are loaded in the JVM to the command line. 这将在JVM中将类加载到命令行时打印出类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM