On scala project - Getting error GC overhead limit exceeded when running sbt test command

I got help from my friends :)

Increase the memory option by executing with -mem option for example:

sbt -mem 2048 test

Other options:

For Mac & Linux user:

if we need to execute this a lot. We can update the .bash_profile file and add below command:

export SBT_OPTS="-Xmx2G"

Other solution (works with Windows as well):

There's also a specific sbtopts file where you can persist this memory setting:

Find a file in Mac/Linux: /usr/local/etc/sbtopts Or in Windows C:\Program Files (x86)\sbt\conf

and add below configuration:

# set memory options
#
-mem   2048

Hopefully any of these tips will help someone with this problem.


Having a look at the launcher script for running sbt, which on my system resides at /usr/share/sbt/bin/sbt, we see the following:

declare -r sbt_opts_file=".sbtopts"
declare -r etc_sbt_opts_file="/etc/sbt/sbtopts"
declare -r dist_sbt_opts_file="${sbt_home}/conf/sbtopts"

...

# Here we pull in the default settings configuration.
[[ -f "$dist_sbt_opts_file" ]] && set -- $(loadConfigFile "$dist_sbt_opts_file") "$@"

# Here we pull in the global settings configuration.
[[ -f "$etc_sbt_opts_file" ]] && set -- $(loadConfigFile "$etc_sbt_opts_file") "$@"

#  Pull in the project-level config file, if it exists.
[[ -f "$sbt_opts_file" ]] && set -- $(loadConfigFile "$sbt_opts_file") "$@"

#  Pull in the project-level java config, if it exists.
[[ -f ".jvmopts" ]] && export JAVA_OPTS="$JAVA_OPTS $(loadConfigFile .jvmopts)"

run "$@"

Thus we can put configuration settings in:

.jvmopts
.sbtopts
/etc/sbt/sbtopts
${sbt_home}/conf/sbtopts

For example, typelevel/cats project uses .jvmopts to set -Xmx3G. Alternatively we could do

echo "-mem 2048" >> .sbtopts

Regarding environmental variablessbt -h documents that

  JAVA_OPTS          environment variable, if unset uses "$java_opts"
  .jvmopts           if this file exists in the current directory, its contents
                     are appended to JAVA_OPTS
  SBT_OPTS           environment variable, if unset uses "$default_sbt_opts"
  .sbtopts           if this file exists in the current directory, its contents
                     are prepended to the runner args

For example,

export JAVA_OPTS=-Xmx2G
sbt

should run sbt with 2G of memory.

Note that if you are running tests in forked JVM, then you can increase memory via javaOptions setting in build.sbt like so:

Test / fork := true
Test / javaOptions ++= Seq("-Xmx4G")

VisualVM is a useful tool to see what settings were passed to a JVM process when experimenting with different ways of configuring SBT.