Search code examples
scalasbt

Set the stack size for SBT


I'm running SBT using my specially built Scala. My built Scala Compiler will do a lot of things at runtime, with a lot of function calls, which can be recursive.

So when I run SBT using my built Scala Compiler, stack overflows after a long time. I try to set -J-Xss when starting SBT. But that doesn't work.

I encountered the problem with SBT heap size before. And many posts says setting -J-Xmx when starting SBT won't change the JVM heap size because it is overridden by the default SBT memory options. How to set heap size for sbt?

Now, I wonder whether -J-Xss can be overridden by default SBT options, just like -J-Xmx being overridden. Or I should simply try to set -J-Xss larger?


Solution

  • There are a number of ways to do this, but it depends what you are trying to achieve. If you want larger heap for running tests for instance, look at the secondary approach undertaken here.

    SBT_OPTS

    First you can simply set the environment variable SBT_OPTS which SBT will natively look for while loading itself, and this should override any settings that you want to specify.

    export SBT_OPTS = "-Xmx1G;-Xms256m;...";
    

    Custom launcher

    The other way to achieve the same is to basically create a custom SBT launching script. Have a look at the example here.

    For testing

    If you want to modify the testing options, you need to use javaOptions in ThisBuild ++= Seq("-Xmx1g", ...). For them to even be run, you always need to have fork in Test := true, which will create a forked JVM for running tests. Without that, the options specified will not be honoured.