How to set heap size for sbt?

I am using SBT 0.12.0. I have read other answers on stack overflow and followed them, however none of them helps, for example:

  • create ForkRun class - I have not observed any forked process during my usage of sbt
  • set environment variable JAVA_OPTS - it is set but sbt's process command line does not seem to use it at all.
  • sbt -J-Xmx2G appends the parameter to sbt process command line, however the old value -Xmx1536m is used by sbt instead of the appended parameter.

Am I missing something? How do I set heap size for sbt 0.12, when doing both testing and run?


Solution 1:

You need SBT_OPTS, here's what I use in my .bash_profile:

export SBT_OPTS="-Xmx1536M -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xss2M  -Duser.timezone=GMT"

UPDATE: To get your 2G heap space you can use this:

export SBT_OPTS="-Xmx2G -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xss2M  -Duser.timezone=GMT"

NOTE: SBT MUST BE LATEST VERSION

Older versions of sbt contain bugs that override these settings, use brew upgrade sbt for latest sbt for Mac (assuming brew install) (IDK for Linux). https://github.com/sbt/sbt/issues/2945#issuecomment-277490848

Solution 2:

As of March 2015, if you are using sbt on OSX with Homebrew then you should edit the file /usr/local/etc/sbtopts

e.g.

# set memory options
#
#-mem   <integer>
-mem 2048

Solution 3:

"sbt -mem 23000 run" works for me.

Solution 4:

I have found the solution. No matter how you specify JVM heap size, it will never work because SBT executable already has it overridden.

There is a line in SBT executable which says:

. /usr/share/sbt/sbt-launch-lib.bash

So I edited the file:

  # run sbt
  execRunner "$java_cmd" \
    ${SBT_OPTS:-$default_sbt_opts} \
-   $(get_mem_opts $sbt_mem) \
    ${java_opts} \
    ${java_args[@]} \
    -jar "$sbt_jar" \
    "${sbt_commands[@]}" \
    "${residual_args[@]}"

Remove the - line.

Now when you run SBT, it will no longer override your JVM heap size settings. You can specify heap size settings using @Noan's answer.

Or alternatively:

sbt -J-Xmx4G -J-Xms4G