scala 如何指定 JVM 最大堆大小“-Xmx”以在 SBT 中使用“运行”操作运行应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3868863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify JVM maximum heap size "-Xmx" for running an application with "run" action in SBT?
提问by Ivan
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?
我的应用程序处理大型数据数组,并且需要比 JVM 默认提供的更多内存。我知道在 Java 中它是由“-Xmx”选项指定的。如何设置 SBT 以使用特定的“-Xmx”值来运行具有“运行”操作的应用程序?
采纳答案by Arne
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)
})
}
回答by iwein
For forked processes you should look at Build.scala
对于分叉进程,您应该查看 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:
要修改分叉进程的 java 选项,您需要在 Build.scala(或任何您命名的构建)中指定它们,如下所示:
val buildSettings = Defaults.defaultSettings ++ Seq(
//…
javaOptions += "-Xmx1G",
//…
)
This will give you the proper options without modifying JAVA_OPTS globally, andit will put custom JAVA_OPTS in an sbt generated start-script
这会给你正确的选项,而全球范围内修改JAVA_OPTS,并且它将把自定义JAVA_OPTS在SBT生成的启动脚本
For non forkedprocesses it's most convenient to set the config via sbtoptsor sbtconfigdepending on your sbt version.
对于非分叉进程,通过sbtopts或sbtconfig根据您的 sbt 版本设置配置是最方便的。
Since sbt 0.13.6 .sbtconfigis deprecated. Modify /usr/local/etc/sbtoptsalong these lines:
由于 sbt 0.13.6.sbtconfig已弃用。/usr/local/etc/sbtopts沿着这些线修改:
-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 alsocreate an .sbtoptsfile in the root of your SBT project using the same syntax as in the /usr/local/etc/sbtoptsfile. This makes the project self-contained.
您还可以使用与.sbtopts文件中相同的语法在 SBT 项目的根目录中创建一个/usr/local/etc/sbtopts文件。这使得项目自包含。
Before sbt 0.13.6you could set the options in .sbtconfig for non forkedprocesses:
在 sbt 0.13.6 之前,您可以在 .sbtconfig 中为非分叉进程设置选项:
Check where sbt is:
$ which sbt /usr/local/bin/sbtLook 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"
检查 sbt 在哪里:
$ which sbt /usr/local/bin/sbt看内容:
$ 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 "$@"设置正确的 jvm 选项以防止 OOM(常规和 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=".." sbtas 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.
如果您只想为当前运行的 sbt 设置 SBT_OPTS,您可以env SBT_OPTS=".." sbt按照 Googol Shan 的建议使用。或者您可以使用在 Sbt 12: 中添加的选项sbt -mem 2048。对于更长的选项列表,这会变得笨拙,但如果您有不同需求的不同项目,它可能会有所帮助。
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.
请注意,CMSClassUnloadingEnabled 与 UseConcMarkSweepGC 配合有助于保持 PermGen 空间清洁,但根据您使用的框架,您可能在 PermGen 上存在实际泄漏,这最终会强制重新启动。
回答by scrapcodes
In sbt version 12 onwards there is an option for this:
在 sbt 版本 12 之后,有一个选项:
$sbt -mem 2048
回答by Googol Shan
If you run sbt on linux shell, you can use:
如果你在 linux shell 上运行 sbt,你可以使用:
env JAVA_OPTS="-Xmx512m" sbt run
This is my usually used command to run my sbt project.
这是我经常使用的命令来运行我的 sbt 项目。
回答by omnomnom
.sbtconfigis deprecated starting with SBT 0.13.6. Instead, I configured these options in /usr/local/etc/sbtoptsin the following way:
.sbtconfig从 SBT 开始不推荐使用0.13.6。相反,我/usr/local/etc/sbtopts按以下方式配置了这些选项:
-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
回答by Synesso
There's one way I know of. Set the environment variable JAVA_OPTS.
我知道一种方法。设置环境变量 JAVA_OPTS。
JAVA_OPTS='-Xmx512m'
I have not found a way to do this as a command parameter.
我还没有找到将其作为命令参数执行此操作的方法。
回答by Brett
Use JAVA_OPTS for setting with environment variable.
使用 JAVA_OPTS 设置环境变量。
Use -J-X options to sbt for individual options, e.g. -J-Xmx2048 -J-XX:MaxPermSize=512
使用 -JX 选项对单个选项进行 sbt,例如 -J-Xmx2048 -J-XX:MaxPermSize=512
Newer versions of sbt have a "-mem" option.
较新版本的 sbt 有一个“-mem”选项。
回答by Pete Neisen
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.
在javaOptions += "-XX:MaxPermSize=1024"用如上述对我们工作时,我们看到,同时通过SBT运行Specs2测试抛出java.lang.OutOfMemoryError @iwein引用我们build.sbt。
回答by Sajive Kumar
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.
环境变量是_JAVA_OPTIONS,需要设置。一旦您设置了 _JAVA_OPTIONS,并且当您 sbt 时,sbt 将使用 JAVA_OPTIONS 和值显示消息。
Alternatively you could set javaOption in the sbt or .scala file e.g
或者,您可以在 sbt 或 .scala 文件中设置 javaOption 例如
javaOptions += "-Xmx1G"
From sbt shell you could run show javaOptions to see the values that are set.
从 sbt shell 您可以运行 show javaOptions 来查看设置的值。
回答by VasiliNovikov
javaOptions in Test += "-Xmx1G"
This sets the JVM options for tests. Works also with jvm forking (fork in Test := true).
这为测试设置了 JVM 选项。也适用于 jvm 分叉 ( fork in Test := true)。

