How to prevent java.lang.OutOfMemoryError: PermGen

2019-01-10 05:29发布

I have noticed a strange behavior of my scala compiler. It occasionally throws an OutOfMemoryError when compiling a class. Here's the error message:

[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.

Does anybody have a clue as to what might be the cause for this error? Thanks in advance for your insights.

8条回答
做个烂人
2楼-- · 2019-01-10 05:51

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.

查看更多
不美不萌又怎样
3楼-- · 2019-01-10 05:54

I had this issue, played around with it for 10 minutes looking at sites trying to change the memory size.

Turns out i resolved it by,

user-profile$ sbt

Then,

sbt-project-name 0.1> clean

This cleared it up for me.

查看更多
再贱就再见
4楼-- · 2019-01-10 05:55

Originally using a command like:

java -jar /path/to/sbt-launch.jar test

I got first OutOfMemoryError: PermGen space which I solved using -XX:MaxPermSize, and then OutOfMemoryError: Java heap space, to which -Xmx was the remedy.

So in my case, a command like this worked:

java -XX:MaxPermSize=256M -Xmx2048M -jar /path/to/sbt-launch.jar test
查看更多
Deceive 欺骗
5楼-- · 2019-01-10 05:56

The cause for OutOfMemoryError: PermGen space is 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 sbt script. For other JVMs, look at their documentation.

查看更多
不美不萌又怎样
6楼-- · 2019-01-10 06:01

I assumed you're using sbt 0.13.6 or higher. Create .sbtopts file in your sbt project's root with the following content:

-J-Xmx4G
-J-XX:MaxMetaspaceSize=1G
-J-XX:MaxPermSize=1G
-J-XX:+CMSClassUnloadingEnabled

MaxMetaspaceSize is for Java 8 whereas MaxPermSize is for Java 7. They are critical to prevent out of memory errors related either to permgen or metaspace exhaustion. Of course, consider adapting flag values or adding any other flags required.

More details and alternative approaches can be found in this blog post.

查看更多
孤傲高冷的网名
7楼-- · 2019-01-10 06:09

I use HomeBrew to install sbt on OS X. It supports a SBT_OPTS argument which can be put in ~/.sbtconfig file with export SBT_OPTS=-XX:MaxPermSize=256M.

查看更多
登录 后发表回答