How to set heap size for sbt?

ScalaSbt

Scala Problem Overview


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?

Scala Solutions


Solution 1 - Scala

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 - Scala

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 - Scala

"sbt -mem 23000 run" works for me.

Solution 4 - Scala

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

Solution 5 - Scala

On windows, for sbt 0.13.9.2, you need to set JAVA_OPTS to the jvm options you want.

> set JAVA_OPTS=-Xmx1G
> sbt assembly

The sbt.bat script loads its defaults from conf\sbtconfig.txt into CFG_OPTS but will use JAVA_OPTS instead if set.

Relevant excerpts from sbt.bat:

rem FIRST we load the config file of extra options.
set FN=%SBT_HOME%\..\conf\sbtconfig.txt
set CFG_OPTS=
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%FN%") DO (
  set DO_NOT_REUSE_ME=%%i
  rem ZOMG (Part #2) WE use !! here to delay the expansion of
  rem CFG_OPTS, otherwise it remains "" for this loop.
  set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
)

. . . (skip) . . .

rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config.
set _JAVA_OPTS=%JAVA_OPTS%
if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=%CFG_OPTS%
:run
"%_JAVACMD%" %_JAVA_OPTS% %SBT_OPTS% -cp "%SBT_HOME%sbt-launch.jar" xsbt.boot.Boot %*


Solution 6 - Scala

I was looking to solve a problem like this on Mac OS X with a homebrew install of SBT. If you installed SBT via homebrew, you're in the clear since the /usr/local/bin/sbt file looks like

#!/bin/sh
test -f ~/.sbtconfig && . ~/.sbtconfig
exec java -Xmx512M ${SBT_OPTS} -jar /usr/local/Cellar/sbt/0.12.3/libexec/sbt-launch.jar "$@"

This means that any settings you put in SBT_OPTS will stick (your -Xmx will take precedence). Furthermore, the first line of the script will execute any commands in ~/.sbtconfig if it exists so it may be a better place to put your SBT options if you are playing with them quite a bit. You won't have to source ~/.bash_profile every time you make a change to SBT_OPTS

Solution 7 - Scala

A quick way to do it is with a .jvmopts file in the root of your project (from the Lagom Framework documentation):

 $ cat .jvmopts
 -Xms512M
 -Xmx4096M
 -Xss2M
 -XX:MaxMetaspaceSize=1024M

Solution 8 - Scala

If running sbt from PowerShell, set the SBT_OPTs environment variable, like so:

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

Then run:

sbt

Solution 9 - Scala

For SBT version 1.0.4 on Windows the default JVM settings come from sbt\conf\sbtconfig.txt file. Simply edit the values here. Change -Xmx512M to -Xmx2048M.

This is not the only source of JVM options for SBT. Others can be found by inspecting sbt.bat. A simple way to diagnose, where do the settings come from, is by commenting out this line in batch file: @echo off.

Solution 10 - Scala

In my case, the configuration of my service was overwriting the environment variable SBT_OPTS and JAVA_OPTS. I was able to set the limits by setting in my build.sbt the following:

javaOptions in Universal ++= Seq(
  "-J-Xms1g",
  "-J-Xmx2g")

Reference: https://www.scala-sbt.org/sbt-native-packager/archetypes/java_app/customize.html

Solution 11 - Scala

On windows, running SET SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xmx2G -Xms1G" before sbt seems to be what solved it for me.

Reference: https://www.baeldung.com/scala/sbt-heap-size

I just changed export to SET.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser972946View Question on Stackoverflow
Solution 1 - ScalaNoahView Answer on Stackoverflow
Solution 2 - ScalaSynessoView Answer on Stackoverflow
Solution 3 - ScalaGBYView Answer on Stackoverflow
Solution 4 - Scalauser972946View Answer on Stackoverflow
Solution 5 - ScalachoyView Answer on Stackoverflow
Solution 6 - ScalaAdrian RodriguezView Answer on Stackoverflow
Solution 7 - ScalamikeView Answer on Stackoverflow
Solution 8 - ScalasungiantView Answer on Stackoverflow
Solution 9 - ScalaJarekczekView Answer on Stackoverflow
Solution 10 - ScalaCassioView Answer on Stackoverflow
Solution 11 - ScalaLuisa Sinzker FantinView Answer on Stackoverflow