如何在 Scala 编译时防止 java.lang.OutOfMemoryError: PermGen space?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8331135/
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 prevent java.lang.OutOfMemoryError: PermGen space at Scala compilation?
提问by BumbleGee
I have noticed a strange behavior of my scala compiler. It occasionally throws an OutOfMemoryError when compiling a class. Here's the error message:
我注意到我的 Scala 编译器有一个奇怪的行为。它在编译类时偶尔会抛出 OutOfMemoryError 。这是错误消息:
[info] Compiling 1 Scala source to /Users/gruetter/Workspaces/scala/helloscala/target/scala-2.9.0/test-classes...
java.lang.OutOfMemoryError: PermGen space
Error during sbt execution: java.lang.OutOfMemoryError: PermGen space
It only happens once in a while and the error is usually not thrown on the subsequent compile run. I use Scala 2.9.0 and compile via SBT.
它只偶尔发生一次,并且通常不会在随后的编译运行中抛出错误。我使用 Scala 2.9.0 并通过 SBT 编译。
Does anybody have a clue as to what might be the cause for this error? Thanks in advance for your insights.
有没有人知道这个错误的原因可能是什么?预先感谢您的见解。
采纳答案by Alexey Romanov
The cause for OutOfMemoryError: PermGen spaceis that it doesn't have enough permanent generation space:) If you are using Oracle JVM, you need to add the -XX:MaxPermSize=256M(or some other amount of space) argument to your sbtscript. For other JVMs, look at their documentation.
原因OutOfMemoryError: PermGen space是它没有足够的永久代空间:) 如果您使用的是 Oracle JVM,则需要将-XX:MaxPermSize=256M(或其他一些空间量)参数添加到您的sbt脚本中。对于其他 JVM,请查看他们的文档。
回答by michaelliu
I use HomeBrew to install sbt on OS X. It supports a SBT_OPTSargument which can be put in ~/.sbtconfigfile with export SBT_OPTS=-XX:MaxPermSize=256M.
我使用 HomeBrew 在 OS X 上安装 sbt。它支持一个SBT_OPTS参数,可以~/.sbtconfig用export SBT_OPTS=-XX:MaxPermSize=256M.
回答by Tvaroh
I assumed you're using sbt0.13.6 or higher. Create .sbtoptsfile in your sbtproject's root with the following content:
我假设您使用的是sbt0.13.6 或更高版本。使用以下内容.sbtopts在sbt项目的根目录中创建文件:
-J-Xmx4G
-J-XX:MaxMetaspaceSize=1G
-J-XX:MaxPermSize=1G
-J-XX:+CMSClassUnloadingEnabled
MaxMetaspaceSizeis for Java 8 whereas MaxPermSizeis for Java 7. They are critical to prevent out of memory errors related either to permgenor metaspaceexhaustion. Of course, consider adapting flag values or adding any other flags required.
MaxMetaspaceSize适用于 Java 8 而MaxPermSize适用于 Java 7。它们对于防止与permgen或元空间耗尽相关的内存不足错误至关重要。当然,请考虑调整标志值或添加所需的任何其他标志。
More details and alternative approaches can be found in this blog post.
更多细节和替代方法可以在这篇博文中找到。
回答by Alex Stewart
I had this issue, played around with it for 10 minutes looking at sites trying to change the memory size.
我遇到了这个问题,在查看试图更改内存大小的网站上玩了 10 分钟。
Turns out i resolved it by,
结果我解决了,
user-profile$ sbt
Then,
然后,
sbt-project-name 0.1> clean
This cleared it up for me.
这为我清除了它。
回答by Ivan
It looks like a memory leak in SBT for me as in my case the program compiles and runs successfully for about 3-5 times before hitting the exception which is fixed by SBT restart.
对我来说,这看起来像是 SBT 中的内存泄漏,因为在我的情况下,程序在遇到由 SBT 重启修复的异常之前成功编译并运行了大约 3-5 次。
The most adequate solution indeed seems to be -XX:MaxPermSize=JVM parameter as Alexey Romanov suggests or to restart SBT periodically if it helps.
最合适的解决方案确实似乎是-XX:MaxPermSize=JVM 参数,正如 Alexey Romanov 建议的那样,或者如果有帮助,则定期重新启动 SBT。
But there is another interesting way: try switching to Java 8. AFAIK it doesn't use PermGen any more and is probably immune to this exception this way.
但是还有另一种有趣的方式:尝试切换到Java 8。AFAIK 它不再使用 PermGen 并且可能以这种方式不受此异常的影响。
I still hope SBT authors will address this issue in future versions.
我仍然希望 SBT 作者能在未来的版本中解决这个问题。
回答by niebo
I am building with the Jenkins sbt plugin and had the same problems. They were resolved after copying the SBT_OPTS from the sbt file to the Jenkins job config's JVM flags.
我正在使用 Jenkins sbt 插件构建并且遇到了同样的问题。在将 SBT_OPTS 从 sbt 文件复制到 Jenkins 作业配置的 JVM 标志后,它们得到了解决。
回答by Jonik
Originally using a command like:
最初使用如下命令:
java -jar /path/to/sbt-launch.jar test
I got first OutOfMemoryError: PermGen spacewhich I solved using -XX:MaxPermSize, and then OutOfMemoryError: Java heap space, to which -Xmxwas the remedy.
我首先得到 OutOfMemoryError: PermGen space,我使用它解决了-XX:MaxPermSize,然后是 OutOfMemoryError: Java heap space,这-Xmx是补救措施。
So in my case, a command like this worked:
所以就我而言,这样的命令有效:
java -XX:MaxPermSize=256M -Xmx2048M -jar /path/to/sbt-launch.jar test
回答by KARTHIKEYAN.A
change following code block in sbt.sh file and save its working fine.
更改 sbt.sh 文件中的以下代码块并保存其工作正常。
get_mem_opts () {
local mem=${1:-1536}
local perm=$(( $mem / 4 ))
(( $perm > 256 )) || perm=1024 //256 to 1024
(( $perm < 1024 )) || perm=2048 // 1024 to 2048
local codecache=$(( $perm / 2 ))
echo "-Xms${mem}m -Xmx${mem}m -XX:MaxPermSize=${perm}m -XX:ReservedCodeCacheSize=${codecache}m"
}
or
或者
using terminal to export sbt config
使用终端导出 sbt 配置
export SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:PermSize=1024M -XX:MaxPermSize=2048M"

