Java 如何使用 maven jvmArg 解决“超出 GC 开销限制”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18770487/
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
How to solve "GC overhead limit exceeded" using maven jvmArg?
提问by Alexandre Can?ado Cardoso
When running a class I have the following exception:
运行类时,我有以下异常:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
线程“main”中的异常 java.lang.OutOfMemoryError:超出 GC 开销限制
I've tried to increase the jvmArg heap size from inside maven pom.xml of the class package:
我试图从类包的 maven pom.xml 内部增加 jvmArg 堆大小:
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<parent>
(...)
</parent>
<artifactId>(...)</artifactId>
<name>(...)</name>
<properties>
<javaOpts.Xmx>4g</javaOpts.Xmx> <!-- default that can be adjusted on the command line with -DjavaOpts.Xmx=... -->
<(...).basedir>${project.basedir}/..</(...).basedir>
</properties>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<configuration>
<launchers>
<launcher>
<id>MyClassName</id>
<mainClass>(...)</mainClass>
<jvmArgs>
<jvmArg>-Xmx${javaOpts.Xmx}</jvmArg>
(...)
I've tried the last cited line with many values:
我已经尝试了最后引用的具有许多值的行:
<jvmArg>-Xmx512m{javaOpts.Xmx}</jvmArg>
<jvmArg>-Xmx4096M{javaOpts.Xmx}</jvmArg>
- ...
<jvmArg>-Xmx10000000000M{javaOpts.Xmx}</jvmArg>
<jvmArg>-Xmx512m{javaOpts.Xmx}</jvmArg>
<jvmArg>-Xmx4096M{javaOpts.Xmx}</jvmArg>
- ...
<jvmArg>-Xmx10000000000M{javaOpts.Xmx}</jvmArg>
But for all of them I have the same error.
但对于他们所有人,我都有同样的错误。
Anyone can help me? Observation: I'm running from IntelliJ IDEA.
任何人都可以帮助我吗?观察:我正在运行IntelliJ IDEA。
回答by flavian
Java 7
爪哇 7
It's not a Maven issue, you need to give more memory to the VM. You do this via environment variables, and the Maven Launcher will automatically pick up these settings on load, and use them to configure the underlying Java VM.
这不是 Maven 问题,您需要为 VM 提供更多内存。您可以通过环境变量执行此操作,Maven Launcher 将在加载时自动获取这些设置,并使用它们来配置底层 Java VM。
The simple way to do it is to:
这样做的简单方法是:
export MAVEN_OPTS="-Xmx1024M -Xss128M -XX:MaxPermSize=1024M -XX:+CMSClassUnloadingEnabled"
export MAVEN_OPTS="-Xmx1024M -Xss128M -XX:MaxPermSize=1024M -XX:+CMSClassUnloadingEnabled"
Windows:
视窗:
set MAVEN_OPTS=-Xmx1024M -Xss128M -XX:MaxPermSize=1024M -XX:+CMSClassUnloadingEnabled
set MAVEN_OPTS=-Xmx1024M -Xss128M -XX:MaxPermSize=1024M -XX:+CMSClassUnloadingEnabled
(No double quotes)
(没有双引号)
Java 8
爪哇 8
Java 8 has replaced permanent generation with metaspace, and the new settings look like this:
Java 8 已经用元空间取代了永久代,新的设置如下所示:
export MAVEN_OPTS="-Xmx1024M -Xss128M -XX:MetaspaceSize=512M -XX:MaxMetaspaceSize=1024M -XX:+CMSClassUnloadingEnabled"
export MAVEN_OPTS="-Xmx1024M -Xss128M -XX:MetaspaceSize=512M -XX:MaxMetaspaceSize=1024M -XX:+CMSClassUnloadingEnabled"
回答by Nami
In some cases you may disable GC limit:
在某些情况下,您可以禁用 GC 限制:
-XX:-UseGCOverheadLimit
回答by Pablo Mendes
You are adding jvmArgs to the maven-scala-pluginconfig in your pom.xml.
您将 jvmArgs 添加到pom.xml 中的maven-scala-plugin配置。
What do you mean you are running from IntelliJ IDEA? How exactly are you doing that? Via their build/run you are not going via Maven at all! Then changing the maven-scala-plugin launchers in your pom.xml is basically useless. To change the run configurations for IntelliJ see this answer.
您从 IntelliJ IDEA 运行是什么意思?你到底是怎么做的?通过他们的构建/运行,您根本不会通过 Maven!然后在你的 pom.xml 中更改 maven-scala-plugin 启动器基本上是没用的。要更改 IntelliJ 的运行配置,请参阅此答案。
If you really want to run from Maven, after you add the jvmArg params to the pom.xml, you then need to run the plugin:
如果你真的想从 Maven 运行,在你将 jvmArg 参数添加到 pom.xml 之后,你需要运行插件:
mvm scala:run -Dlauncher=MyClassName
Note: where you put MyClassName, that is actually the name of the launcher, not the class. For many projects, people pick reasonable names for the launchers and that makes them often coincide with the local name of the classes. But you could call it MyLauncher if you wanted.
注意:放置 MyClassName 的地方,实际上是启动器的名称,而不是类。对于许多项目,人们会为启动器选择合理的名称,这使得它们通常与类的本地名称一致。但如果您愿意,您可以将其称为 MyLauncher。
Reading maven-scala-plugin's docs might help you.
阅读 maven-scala-plugin 的文档可能会对您有所帮助。
回答by sap1ens
Sorry for very late response, but I think my answer might help other people as well.
很抱歉回复很晚,但我认为我的回答也可能对其他人有所帮助。
scala-maven-plugin doesn't work with MAVEN_OPTS, the only way to increase memory is to use jvmArgs in the plugin configuration. But you should put jvmArgs not inside launcher section, but inside configuration section, like this:
scala-maven-plugin 不适用于 MAVEN_OPTS,增加内存的唯一方法是在插件配置中使用 jvmArgs。但是你不应该把 jvmArgs 放在启动器部分,而是放在配置部分,像这样:
<configuration>
<jvmArgs>
<jvmArg>-Xmx1024m</jvmArg>
<jvmArg>-Xms256m</jvmArg>
</jvmArgs>
</configuration>
This works for me.
这对我有用。
回答by Pablo Rodriguez Bertorello
Adding parallel garbage collection to the top answer above is probably the best option:
将并行垃圾收集添加到上面的最佳答案可能是最好的选择:
-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC
回答by Alexander
I had kind of the same problem: My project wokspace consists of >250 maven modules and when opening a pom.xml
in IDEA, the <project>
and <parent>
tags were underlined in red and this error showed up, when hovering over the broken tags:
我遇到了同样的问题:我的项目 wokspace 由 >250 个 maven 模块组成,当pom.xml
在 IDEA 中打开一个时,<project>
and<parent>
标签用红色下划线标出,当鼠标悬停在损坏的标签上时会出现这个错误:
java.lang.OutOfMemoryError: GC overhead limit exceeded
java.lang.OutOfMemoryError:超出 GC 开销限制
My solution:
我的解决方案:
- set high values in
>Settings >Build, Execution, Deployment >Build Tools >Maven >Importing
- e.g.-Xmx1g
and - change the maven implementation under
>Settings >Build, Execution, Deployment >Build Tools >Maven
(Maven home directory) from(Bundled) Maven 3
to my local maven installation (not the one bundled with IDEA), e.g. to/opt/maven/apache-maven-3.3.9
:
- 设置高值
>Settings >Build, Execution, Deployment >Build Tools >Maven >Importing
- 例如-Xmx1g
和 - 将
>Settings >Build, Execution, Deployment >Build Tools >Maven
(Maven 主目录)下的 maven 实现从(Bundled) Maven 3
我的本地 maven 安装(不是与 IDEA 捆绑的那个)更改,例如/opt/maven/apache-maven-3.3.9
:
Since I had problems with the bundled maven before, I just don't trust the bundled maven.
由于之前捆绑的maven有问题,我只是不相信捆绑的maven。