Scala: How to give SBT more memory (RAM) to work with

As a brief note, I was trying to run a Scala application inside SBT today and kept getting this “out of memory” error:

[error] (run-main-0) java.lang.OutOfMemoryError: GC overhead limit exceeded
java.lang.OutOfMemoryError: GC overhead limit exceeded

The solution to the problem was to allocate more memory when I start SBT. To give SBT more RAM I first issue this command at the command line:

$ export SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xmx2G"

Then start SBT as usual:

$ sbt

When I do that and run a little Scala program to show me the memory usage stats, it shows that I have 1,981 MB available, which helps to confirm that this approach gives SBT more memory to work with.

Related: Lagom SBT memory settings

In a related note I just saw that this Lagom documentation page makes these recommendations about SBT memory settings:

“We recommend you increase the Maximum Metaspace size and the Thread Stack size. These values can be set using -Xss2M -XX:MaxMetaspaceSize=1024M. The exact values may depend on your hardware and your code base.”

They then suggest this command to start SBT for Lagom:

SBT_OPTS="-Xms512M -Xmx1024M -Xss2M -XX:MaxMetaspaceSize=1024M" sbt