how can i change jvm configuration i.e. heap and stack size in a Play framework2 application with Scala
activator run -Xms1G -Xmx2G -server
i have tried this command but jconsole shows that heap size has not changed
please help me
These are Java switches; in Scala prefix them with -J, namely -J-Xms1G and -J-Xmx2G.
Edit
For setting such options in build.sbt you must enable a new jvm forking ,
fork in run := true
and specify the options for instance like this,
javaOptions in run ++= Seq("-Xms1G", "-Xmx2G")
Related
I would like to use full paths in ScalaTest reporter. The value LineInFile.filePathname tells me I should:
Please set the environment variable SCALACTIC_FILL_FILE_PATHNAMES to yes at compile time to enable this feature.
Can I do this from sbt, or do I have to launch SBT with a different environment? Scalac seems to be running in the same JVM as sbt, and there is a question Scala: Unable to set environment variable telling me I cannot modify my own environment.
I have tried following, none of them seems to set the variable for the compiler:
scalacOptions ++= Seq(
"-deprecation", "-unchecked",
"-DSCALACTIC_FILL_FILE_PATHNAMES=yes"
),
Compile / envVars := Map("SCALACTIC_FILL_FILE_PATHNAMES" -> "yes"),
Test / envVars := Map("SCALACTIC_FILL_FILE_PATHNAMES" -> "yes"),
As far as I know, sbt doesn't run scalac in a separate process (which would allow it to set environment variables), so you'd have to run sbt in an environment with that set.
Note that -D sets Java properties. While it's fairly common for software to allow environment variables and properties to be treated similarly (e.g. Lightbend Config), that's not standard.
The mechanism for setting the environment will depend on how you are launching sbt. If using a Bourne-style shell like bash:
export SCALACTIC_FILL_FILE_PATHNAMES=yes
sbt
or
SCALACTIC_FILL_FILE_PATHNAMES=yes sbt
IDEs and CI/CD tooling may launch sbt directly, in which case check the documentation for those tools.
Sup,
I'm trying to setup environment variables for my project in Play2.4 Scala.
I have set variables in run configuration in Intellij.
What's annoying Scala doesn't seem to see those.
I keep getting errors of configuration not specified for keys I used env variables.
When i start the application those shows in the console:
"C:\Program Files\Java\jdk1.8.0_25\bin\java" -Dfile.encoding=UTF8 -DMAIL_PORT=587 -DDB_URI=mongodb://uri -Djline.terminal=none -Dsbt.log.noformat=true -Dsbt.global.base=C:\Users\Haito\AppData\Local\Temp\sbt-global-plugin7stub -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M -classpath C:\Users\Haito\.IntelliJIdea14\config\plugins\Scala\launcher\sbt-launch.jar xsbt.boot.Boot "project root" ~run
And the configuration file:
mongodb.uri = ${?DB_URI}
play.mailer {
host=${?MAIL_HOST}
port=${?MAIL_PORT}
ssl=false
tls=true
user=${?MAIL_USERNAME}
password=${?MAIL_PASSWD}
debug=false
mock=false
}
And i keep getting those:
Missing configuration key 'mongodb.db'!
Of course my problem is not that my mongo driver. My problem is that the config is not being fed with environment variables. Mailer which also uses environment variables for configs. When I paste the actual URI except the ${?DB_URI} it works.
Build:
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.2.play24"
)
libraryDependencies ++= Seq(
"com.typesafe.play" %% "play-mailer" % "3.0.1"
)
This is a bug in IntelliJ. Setting the "Environment variables" field in the Play run configuration sets system properties, not environment variables.
If you configure environment variables manually somewhere like .bash_profile then you can certainly use them in your application.conf like:
db.default.url = ${?DB_URL}
When i start the application those shows in the console:
"C:\Program Files\Java\jdk1.8.0_25\bin\java" -Dfile.encoding=UTF8 -DMAIL_PORT=587 -DDB_URI=mongodb://uri -Djline.terminal=none -Dsbt.log.noformat=true -Dsbt.global.base=C:\Users\Haito\AppData\Local\Temp\sbt-global-plugin7stub -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M -classpath C:\Users\Haito\.IntelliJIdea14\config\plugins\Scala\launcher\sbt-launch.jar xsbt.boot.Boot "project root" ~run
-D doesn't set environment variables, it sets JVM system properties. You should be able to set environment variables from the run configuration as well, but a different part of the dialog.
Also, Play's documentation doesn't say it looks at environment variables at all:
As well as the application.conf file, configuration comes from a couple of other places.
Default settings are loaded from any reference.conf files found on the classpath. Most Play JARs include a reference.conf file with default settings. Settings in application.conf will override settings in reference.conf files.
It’s also possible to set configuration using system properties. System properties override application.conf settings.
I.e. passing -Dmongodb.uri=... should work.
Later on it does say
For substitutions which are not found in the configuration tree, implementations may try to resolve them by looking at system environment variables or other external sources of configuration. (More detail on environment variables in a later section.)
but this is just a quote from HOCON README.
You need to looks at the code loading the config to check if it does use one of the methods which include environment variables.
If your mongodb url contains parameters like mongodb://xxxxxxxxxx?key=value then intellij will silently remove that env variable.
Is there a way to increase the memory during assembly (the plugin)? I notice that adding more memory to sbt through SBT_OPTS does't really help. I suspect that assembly is forking a separate JVM that doesn't inherit the JVM configurations from sbt.
Here is the error:
java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: Java heap space
Here is what I tried without much luck:
javaOptions in assembly += "-Xmx2g"
I suspect that assembly is forking a separate JVM that doesn't inherit the JVM configurations from sbt.
I'm the author of sbt-assembly. assembly does not fork a separate JVM.
You can see the code here and check - https://github.com/sbt/sbt-assembly/blob/0.13.0/src/main/scala/sbtassembly/Assembly.scala#L239-L246
Make sure to export SBT_OPTS. Mine is "-Xmx2G -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xss2M"
I met the same issue before, Please do it as below for windows. The issue could be gone.
set SBT_OPTS="-Xmx2G"
sbt assembly
You can increase heap size by using below mentioned command in linux environment:-
sbt -J-Xms2048m -J-Xmx2048m assembly
I have noticed a strange behavior of my scala compiler. It occasionally throws an OutOfMemoryError when compiling a class. Here's the error message:
[info] Compiling 1 Scala source to /Users/gruetter/Workspaces/scala/helloscala/target/scala-2.9.0/test-classes...
java.lang.OutOfMemoryError: PermGen space
Error during sbt execution: java.lang.OutOfMemoryError: PermGen space
It only happens once in a while and the error is usually not thrown on the subsequent compile run. I use Scala 2.9.0 and compile via SBT.
Does anybody have a clue as to what might be the cause for this error? Thanks in advance for your insights.
I use HomeBrew to install sbt on OS X. It supports a SBT_OPTS argument which can be put in ~/.sbtconfig file with export SBT_OPTS=-XX:MaxPermSize=256M.
The cause for OutOfMemoryError: PermGen space is that it doesn't have enough permanent generation space :) If you are using Oracle JVM, you need to add the -XX:MaxPermSize=256M (or some other amount of space) argument to your sbt script. For other JVMs, look at their documentation.
I assumed you're using sbt 0.13.6 or higher. Create .sbtopts file in your sbt project's root with the following content:
-J-Xmx4G
-J-XX:MaxMetaspaceSize=1G
-J-XX:MaxPermSize=1G
-J-XX:+CMSClassUnloadingEnabled
MaxMetaspaceSize is for Java 8 whereas MaxPermSize is for Java 7. They are critical to prevent out of memory errors related either to permgen or metaspace exhaustion. Of course, consider adapting flag values or adding any other flags required.
More details and alternative approaches can be found in this blog post.
I had this issue, played around with it for 10 minutes looking at sites trying to change the memory size.
Turns out i resolved it by,
user-profile$ sbt
Then,
sbt-project-name 0.1> clean
This cleared it up for me.
It looks like a memory leak in SBT for me as in my case the program compiles and runs successfully for about 3-5 times before hitting the exception which is fixed by SBT restart.
The most adequate solution indeed seems to be -XX:MaxPermSize= JVM parameter as Alexey Romanov suggests or to restart SBT periodically if it helps.
But there is another interesting way: try switching to Java 8. AFAIK it doesn't use PermGen any more and is probably immune to this exception this way.
I still hope SBT authors will address this issue in future versions.
I am building with the Jenkins sbt plugin and had the same problems. They were resolved after copying the SBT_OPTS from the sbt file to the Jenkins job config's JVM flags.
Originally using a command like:
java -jar /path/to/sbt-launch.jar test
I got first OutOfMemoryError: PermGen space which I solved using -XX:MaxPermSize, and then OutOfMemoryError: Java heap space, to which -Xmx was the remedy.
So in my case, a command like this worked:
java -XX:MaxPermSize=256M -Xmx2048M -jar /path/to/sbt-launch.jar test
change following code block in sbt.sh file and save its working fine.
get_mem_opts () {
local mem=${1:-1536}
local perm=$(( $mem / 4 ))
(( $perm > 256 )) || perm=1024 //256 to 1024
(( $perm < 1024 )) || perm=2048 // 1024 to 2048
local codecache=$(( $perm / 2 ))
echo "-Xms${mem}m -Xmx${mem}m -XX:MaxPermSize=${perm}m -XX:ReservedCodeCacheSize=${codecache}m"
}
or
using terminal to export sbt config
export SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:PermSize=1024M -XX:MaxPermSize=2048M"
You can also add a .jvmopts file in the root folder of your project, and write inside the file the following:
-Xms1g
-Xmx4g
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?
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:
Check where sbt is:
$ which sbt
/usr/local/bin/sbt
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 "$#"
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.
In sbt version 12 onwards there is an option for this:
$sbt -mem 2048
If you run sbt on linux shell, you can use:
env JAVA_OPTS="-Xmx512m" sbt run
This is my usually used command to run my sbt project.
.sbtconfig is deprecated starting with SBT 0.13.6. Instead, I configured these options in /usr/local/etc/sbtopts in the following way:
-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
Try this:
class ForkRun(info: ProjectInfo) extends DefaultProject(info) {
override def fork = Some(new ForkScalaRun {
override def runJVMOptions = super.runJVMOptions ++ Seq("-Xmx512m")
override def scalaJars = Seq(buildLibraryJar.asFile, buildCompilerJar.asFile)
})
}
There's one way I know of. Set the environment variable JAVA_OPTS.
JAVA_OPTS='-Xmx512m'
I have not found a way to do this as a command parameter.
Use JAVA_OPTS for setting with environment variable.
Use -J-X options to sbt for individual options, e.g. -J-Xmx2048 -J-XX:MaxPermSize=512
Newer versions of sbt have a "-mem" option.
The javaOptions += "-XX:MaxPermSize=1024" in our build.sbt as referenced by #iwein above worked for us when we were seeing a java.lang.OutOfMemoryError thrown while running Specs2 tests through sbt.
The environment variable is _JAVA_OPTIONS, which needs to be set.
Once you set _JAVA_OPTIONS, and when you sbt, sbt will show the message using JAVA_OPTIONS and the values.
Alternatively you could set javaOption in the sbt or .scala file
e.g
javaOptions += "-Xmx1G"
From sbt shell you could run show javaOptions to see the values that are set.
sbt lets you list the JVM options you need to run your project on a file named
.jvmopts
in the root of your project.
then add the java options that you want
cat .jvmopts
-Xms512M
-Xmx4096M
-Xss2M
-XX:MaxMetaspaceSize=1024M
it is tested and works in windows 10
https://www.lagomframework.com/documentation/1.4.x/scala/JVMMemoryOnDev.html
javaOptions in Test += "-Xmx1G"
This sets the JVM options for tests. Works also with jvm forking (fork in Test := true).