Java 如何防止 Netbeans 中的 PermGen 空间错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:43:26  来源:igfitidea点击:

How can I prevent PermGen space errors in Netbeans?

javanetbeansnetbeans6.5java-6

提问by Daniel Rikowski

Every 15-30 minutes Netbeans shows a "java.lang.OutOfMemoryError: PermGen space". From what I learned from Google this seems to be related to classloader leaks or memory leaks in general.

每 15-30 分钟 Netbeans 显示一个“ java.lang.OutOfMemoryError: PermGen space”。从我从谷歌学到的,这似乎与一般的类加载器泄漏或内存泄漏有关。

Unfortunately all suggestions I found were related to application servers and I have no idea to adapted them to Netbeans. (I'm not even sure it's the same problem)

不幸的是,我发现的所有建议都与应用程序服务器有关,我不知道将它们调整到 Netbeans。(我什至不确定这是同样的问题)

Is it a problem in my application? How can I find the source?

我的应用程序有问题吗?我怎样才能找到来源?

采纳答案by ?ukasz Bownik

It is because of constant class loading.

这是因为不断的类加载。

Java stores class byte code and all the constants (e.g. string constants) in permanent heap that is not garbage collected by default (which make sense in majority of situations because classes are loaded only once during the lifetime of an application).

Java 将类字节码和所有常量(例如字符串常量)存储在默认情况下不会被垃圾收集的永久堆中(这在大多数情况下是有意义的,因为类在应用程序的生命周期内仅加载一次)。

In applications that often load classes during an entire lifetime that are:

在通常在整个生命周期内加载类的应用程序中:

  • web and application servers during hot redeployment;
  • IDE's when running developed applications (every time you hit Run button in Netbeans or eclipse it loads your application's classes a new);
  • etc this behavior is improper because a heap fills full eventually.
  • 热重新部署期间的 Web 和应用程序服务器;
  • IDE 在运行开发的应用程序时(每次您在 Netbeans 或 Eclipse 中点击运行按钮时,它都会加载您的应用程序的新类);
  • 等这种行为是不正确的,因为堆最终会填满。

You need to turn on permanent heap garbage collection to prevent this error.

您需要打开永久堆垃圾收集以防止此错误。

I use options

我使用选项

-XX:MaxPermSize=256M
-XX:+CMSClassUnloadingEnabled
-XX:+CMSPermGenSweepingEnabled

(stopped my eclipse 3.4 from throwing "java.lang.OutOfMemoryError: PermGen space" so it should also work with netbeans).

(阻止我的 eclipse 3.4 抛出“ java.lang.OutOfMemoryError: PermGen space”,因此它也应该与 netbeans 一起使用)。

Edit: Just note that for Netbeans you set those options in: [Netbeans installation]\etc\netbeans.confYou should prefixe those options with -Jand add them in netbeans_default_options(see comments in netbeans.conffor more informations).

编辑:请注意,对于 Netbeans,您将这些选项设置在: [Netbeans installation]\etc\netbeans.conf您应该为这些选项-J添加前缀并将它们添加进来netbeans_default_optionsnetbeans.conf有关更多信息,请参阅注释)。

回答by kgiannakakis

See this linkon how to set the size of PermSize. Most probably this isn't a problem of your code, so the only solution would be to increase the the PermSize. I have found that this is quite often, when you work with JAXB. In general, if you use many libraries that themselves also depend on many other jar files, the default size of the PermGen space may not be big enough.

请参阅此链接了解如何设置 PermSize 的大小。这很可能不是您的代码的问题,因此唯一的解决方案是增加 PermSize。我发现当您使用 JAXB 时,这种情况很常见。一般来说,如果你使用了很多库,而这些库本身也依赖于很多其他的jar文件,那么PermGen空间的默认大小可能不够大。

See these blog posts for more details: Classloader leaksand How to fix the dreaded "java.lang.OutOfMemoryError: PermGen space"

有关更多详细信息,请参阅这些博客文章:类加载器泄漏如何修复可怕的“java.lang.OutOfMemoryError: PermGen space”

回答by Maurice Perry

You can change the startup options of netbeans JVM by editing the file /etc/netbeans.conf. I often have this kind of errors when I develop a Webapp and I deploy it often. In that case, I restart tomcat from time to time.

您可以通过编辑文件 /etc/netbeans.conf 来更改 netbeans JVM 的启动选项。我在开发 Webapp 并经常部署时经常会遇到这种错误。在这种情况下,我会不时重启 tomcat。

回答by jassuncao

Try to add the following argument to netbeans netconf: -J-XX:MaxPermSize=256m

尝试将以下参数添加到 netbeans netconf: -J-XX:MaxPermSize=256m

回答by shereifhawary

If you are developing a web application, try to put on server vm option

如果您正在开发 Web 应用程序,请尝试添加服务器 vm 选项

-Xmx512m -Xms512m -XX:MaxPermSize=512m

-Xmx512m -Xms512m -XX:MaxPermSize=512m

Go to Tools/Servers//Platform/VM Options (Netbeans 7.4)

转至工具/服务器//平台/VM 选项(Netbeans 7.4)