SBT Specify java heap size in Build.scala
Asked Answered
G

1

5

My Build.scala file contents.

val commonSettings = Seq(
version := "1.0.0",
organization := "com.collective",
scalaVersion := "2.11.4",
scalacOptions ++= List(
  "-encoding", "UTF-8",
  "-target:jvm-1.7",
  "-feature",
  "-unchecked",
  "-deprecation",
  "-Xlint",
  "-Xfatal-warnings"
),
resolvers ++= Seq(
  "ConJars" at "http://conjars.org/repo"
),
javaOptions in run += "-Xms256M -Xmx2G -XX:MaxPermSize=1024M -XX:+UseConcMarkSweepGC"
)

lazy val segmentFetcher = Project("segments-fetcher", file("."))
.settings(commonSettings: _*)
)

However, when I execute

sbt run

and look in the jconsole, the heap size I set in the Build.scala is not picked up. It just shows -Xmx512M. Can anyone please let me know how we force sbt to pick the heap space from the project build file?

Thanks!

Gypsum answered 9/12, 2014 at 6:7 Comment(0)
Z
10

If you want to change the heap size, you need to tell sbt to fork another JVM (this applies to any JVM option by the way). You can use the fork setting to do so:

fork in run := true

Also, you want to make sure, that your options are properly separated (there is no additional parsing done by sbt):

javaOptions in run ++= Seq(
    "-Xms256M", "-Xmx2G", "-XX:MaxPermSize=1024M", "-XX:+UseConcMarkSweepGC")

Alternatively, you can set the heap size in the sbt runner. If you are using normal sbt, you can modify the launcher script directly. If you are using sbt-extras, you can pass arguments to the JVM using the -J flag:

sbt -J-Xmx2G # etc.
Zingaro answered 9/12, 2014 at 6:56 Comment(3)
when I do fork in run := true, javaOptions in run += "-Xms256m -Xmx1024m -XX:+UseConcMarkSweepGC", I get [error] Invalid initial heap size: -Xms256m -Xmx1024m -XX:+UseConcMarkSweepGC [error] Error: Could not create the Java Virtual MachineGypsum
Oh... didn't realize that your options were wrong (sbt does not parse them into multiple arguments). Updated my answerZingaro
Not a valid command: J-Xms1024M Not a valid project ID: J-Xms1024M Expected ':' (if selecting a configuration) Not a valid key: J-Xms1024M J-Xms1024MSevern

© 2022 - 2024 — McMap. All rights reserved.