Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify JVM maximum heap size "-Xmx" for running an application with "run" action in SBT?

Tags:

scala

sbt

My application does large data arrays processing and needs more memory than JVM gives by default. I know in Java it's specified by "-Xmx" option. How do I set SBT up to use particular "-Xmx" value to run an application with "run" action?

like image 388
Ivan Avatar asked Oct 06 '10 01:10

Ivan


People also ask

Which JVM parameters is used to set the maximum heap size?

-xmx and -xms are the parameters used to adjust the heap size. -Xms: It is used for setting the initial and minimum heap size. It is recommended to set the minimum heap size equivalent to the maximum heap size in order to minimize the garbage collection. -Xmx: It is used for setting the maximum heap size.

What is the maximum recommend JVM heap size?

The theoretical limit is 2^64 bytes, which is 16 exabytes (1 exabyte = 1024 petabytes, 1 petabyte = 1024 terabytes). However, most OS's can't handle that. For instance, Linux can only support 64 terabytes of data. Note: We don't recommend you exceed 2 GB of in use JVM heap.

How do you set the maximum and minimum value of JVM memory?

use the arguments -Xms<memory> -Xmx<memory> . Use M or G after the numbers for indicating Megs and Gigs of bytes respectively. -Xms indicates the minimum and -Xmx the maximum. you may want to look at MaxPermSize as well.


2 Answers

For forked processes you should look at Build.scala

To modify the java options for forked processes you need to specify them in the Build.scala (or whatever you've named your build) like this:

val buildSettings = Defaults.defaultSettings ++ Seq(
   //…
   javaOptions += "-Xmx1G",
   //…
)

This will give you the proper options without modifying JAVA_OPTS globally, and it will put custom JAVA_OPTS in an sbt generated start-script

For non forked processes it's most convenient to set the config via sbtopts or sbtconfig depending on your sbt version.

Since sbt 0.13.6 .sbtconfig is deprecated. Modify /usr/local/etc/sbtopts along these lines:

-J-Xms512M
-J-Xmx3536M
-J-Xss1M
-J-XX:+CMSClassUnloadingEnabled
-J-XX:+UseConcMarkSweepGC
-J-XX:MaxPermSize=724M
-J-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

You can also create an .sbtopts file in the root of your SBT project using the same syntax as in the /usr/local/etc/sbtopts file. This makes the project self-contained.

Before sbt 0.13.6 you could set the options in .sbtconfig for non forked processes:

  1. Check where sbt is:

    $ which sbt
    /usr/local/bin/sbt
    
  2. Look at the contents:

    $ cat /usr/local/bin/sbt
    #!/bin/sh
    test -f ~/.sbtconfig && . ~/.sbtconfig
    exec java ${SBT_OPTS} -jar /usr/local/Cellar/sbt/0.12.1/libexec/sbt-launch.jar "$@"
    
  3. Set the correct jvm options to prevent OOM (both regular and PermGen):

    $ cat ~/.sbtconfig
    SBT_OPTS="-Xms512M -Xmx3536M -Xss1M 
     -XX:+CMSClassUnloadingEnabled 
     -XX:+UseConcMarkSweepGC -XX:MaxPermSize=724M"
    

If you want to set SBT_OPTS only for the current run of sbt you can use env SBT_OPTS=".." sbt as suggested by Googol Shan. Or you can use the option added in Sbt 12: sbt -mem 2048. This gets unwieldy for longer lists of options, but it might help if you have different projects with different needs.

Note that CMSClassUnloadingEnabled in concert with UseConcMarkSweepGC helps keep the PermGen space clean, but depending on what frameworks you use you might have an actual leak on PermGen, which eventually forces a restart.

like image 142
11 revs, 2 users 99% Avatar answered Oct 21 '22 02:10

11 revs, 2 users 99%


In sbt version 12 onwards there is an option for this:

$sbt -mem 2048 
like image 72
Prashant Sharma Avatar answered Oct 21 '22 02:10

Prashant Sharma