scala 如何设置 sbt 的堆大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15280839/
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 set heap size for sbt?
提问by Noah
I am using SBT 0.12.0. I have read other answers on stack overflow and followed them, however none of them helps, for example:
我正在使用 SBT 0.12.0。我已经阅读了有关堆栈溢出的其他答案并遵循了它们,但是它们都没有帮助,例如:
- create
ForkRunclass - I have not observed any forked process during my usage of sbt - set environment variable
JAVA_OPTS- it is set but sbt's process command line does not seem to use it at all. sbt -J-Xmx2Gappends the parameter to sbt process command line, however the old value-Xmx1536mis used by sbt instead of the appended parameter.
- 创建
ForkRun类 - 我在使用 sbt 期间没有观察到任何分叉过程 - 设置环境变量
JAVA_OPTS- 它已设置,但 sbt 的进程命令行似乎根本不使用它。 sbt -J-Xmx2G将参数附加到 sbt 进程命令行,但是-Xmx1536msbt 使用旧值而不是附加参数。
Am I missing something? How do I set heap size for sbt 0.12, when doing both testing and run?
我错过了什么吗?在进行测试和测试时,如何为 sbt 0.12 设置堆大小run?
采纳答案by GBY
I have found the solution. No matter how you specify JVM heap size, it will never work because SBT executable already has it overridden.
我找到了解决办法。无论您如何指定 JVM 堆大小,它都不会起作用,因为 SBT 可执行文件已经覆盖了它。
There is a line in SBT executable which says:
SBT 可执行文件中有一行表示:
. /usr/share/sbt/sbt-launch-lib.bash
. /usr/share/sbt/sbt-launch-lib.bash
So I edited the file:
所以我编辑了文件:
# run sbt
execRunner "$java_cmd" \
${SBT_OPTS:-$default_sbt_opts} \
- $(get_mem_opts $sbt_mem) \
${java_opts} \
${java_args[@]} \
-jar "$sbt_jar" \
"${sbt_commands[@]}" \
"${residual_args[@]}"
Remove the -line.
删除-线。
Now when you run SBT, it will no longer override your JVM heap size settings. You can specify heap size settings using @Noan's answer.
现在,当您运行 SBT 时,它将不再覆盖您的 JVM 堆大小设置。您可以使用@Noan 的回答来指定堆大小设置。
Or alternatively:
或者:
sbt -J-Xmx4G -J-Xms4G
sbt -J-Xmx4G -J-Xms4G
回答by Noah
You need SBT_OPTS, here's what I use in my .bash_profile:
你需要SBT_OPTS,这是我在我的.bash_profile 中使用的:
export SBT_OPTS="-Xmx1536M -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xss2M -Duser.timezone=GMT"
UPDATE:To get your 2G heap space you can use this:
更新:要获得 2G 堆空间,您可以使用:
export SBT_OPTS="-Xmx2G -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xss2M -Duser.timezone=GMT"
NOTE: SBT MUST BE LATEST VERSION
注意:SBT 必须是最新版本
Older versions of sbtcontain bugs that override these settings, use brew upgrade sbtfor latest sbtfor Mac (assuming brew install) (IDK for Linux). https://github.com/sbt/sbt/issues/2945#issuecomment-277490848
旧版本sbt包含覆盖这些设置的错误,用于 Mac 的brew upgrade sbt最新sbt版本(假设 brew 安装)(Linux 的 IDK)。https://github.com/sbt/sbt/issues/2945#issuecomment-277490848
回答by Synesso
As of March 2015, if you are using sbt on OSXwith Homebrewthen you should edit the file /usr/local/etc/sbtopts
截至 2015 年 3 月,如果您在带有Homebrew 的OSX上使用 sbt,那么您应该编辑该文件/usr/local/etc/sbtopts
e.g.
例如
# set memory options
#
#-mem <integer>
-mem 2048
回答by GBY
"sbt -mem 23000 run" works for me.
“sbt -mem 23000 run”对我有用。
回答by Choy
On windows, for sbt 0.13.9.2, you need to set JAVA_OPTSto the jvm options you want.
在 Windows 上,对于 sbt 0.13.9.2,您需要设置JAVA_OPTS为您想要的 jvm 选项。
> set JAVA_OPTS=-Xmx1G
> sbt assembly
The sbt.batscript loads its defaults from conf\sbtconfig.txtinto CFG_OPTSbut will use JAVA_OPTSinstead if set.
该sbt.bat脚本从conf\sbtconfig.txtinto加载其默认值,CFG_OPTS但JAVA_OPTS如果设置则将使用。
Relevant excerpts from sbt.bat:
相关摘录自sbt.bat:
rem FIRST we load the config file of extra options.
set FN=%SBT_HOME%\..\conf\sbtconfig.txt
set CFG_OPTS=
FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%FN%") DO (
set DO_NOT_REUSE_ME=%%i
rem ZOMG (Part #2) WE use !! here to delay the expansion of
rem CFG_OPTS, otherwise it remains "" for this loop.
set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
)
. . . (skip) . . .
. . . (跳过) 。. .
rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config.
set _JAVA_OPTS=%JAVA_OPTS%
if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=%CFG_OPTS%
:run
"%_JAVACMD%" %_JAVA_OPTS% %SBT_OPTS% -cp "%SBT_HOME%sbt-launch.jar" xsbt.boot.Boot %*
回答by Adrian Rodriguez
I was looking to solve a problem like this on Mac OS X with a homebrew install of SBT. If you installed SBT via homebrew, you're in the clear since the /usr/local/bin/sbtfile looks like
我希望通过 SBT 的自制软件安装在 Mac OS X 上解决这样的问题。如果你通过自制软件安装了 SBT,你就清楚了,因为/usr/local/bin/sbt文件看起来像
#!/bin/sh
test -f ~/.sbtconfig && . ~/.sbtconfig
exec java -Xmx512M ${SBT_OPTS} -jar /usr/local/Cellar/sbt/0.12.3/libexec/sbt-launch.jar "$@"
This means that any settings you put in SBT_OPTSwill stick (your -Xmx will take precedence). Furthermore, the first line of the script will execute any commands in ~/.sbtconfigif it exists so it may be a better place to put your SBT options if you are playing with them quite a bit. You won't have to source ~/.bash_profileevery time you make a change to SBT_OPTS
这意味着您输入的任何设置都SBT_OPTS将保留(您的 -Xmx 将优先)。此外,脚本的第一行将执行任何命令(~/.sbtconfig如果存在),因此如果您经常使用它们,它可能是放置 SBT 选项的更好位置。您不必source ~/.bash_profile每次更改SBT_OPTS
回答by sungiant
If running sbt from PowerShell, set the SBT_OPTsenvironment variable, like so:
如果从 PowerShell 运行 sbt,请设置SBT_OPTs环境变量,如下所示:
$env:SBT_OPTS="-Xms512M -Xmx1024M -Xss2M -XX:MaxMetaspaceSize=1024M"
Then run:
然后运行:
sbt
回答by Jarekczek
For SBT version 1.0.4 on Windowsthe default JVM settings come from sbt\conf\sbtconfig.txtfile. Simply edit the values here. Change -Xmx512Mto -Xmx2048M.
对于Windows 上的SBT 1.0.4 版,默认的JVM 设置来自sbt\conf\sbtconfig.txt文件。只需在此处编辑值。更改-Xmx512M为-Xmx2048M。
This is not the only source of JVM options for SBT. Others can be found by inspecting sbt.bat. A simple way to diagnose, where do the settings come from, is by commenting out this line in batch file: @echo off.
这不是 SBT 的 JVM 选项的唯一来源。其他的可以通过检查找到sbt.bat。一个简单的方法来诊断,你在哪里的设置从何而来,是通过注释掉批处理文件中这一行:@echo off。
回答by mike
A quick way to do it is with a .jvmoptsfile in the root of your project (from the Lagom Framework documentation):
一种快速的方法是使用.jvmopts项目根目录中的文件(来自Lagom 框架文档):
$ cat .jvmopts
-Xms512M
-Xmx4096M
-Xss2M
-XX:MaxMetaspaceSize=1024M
回答by Cassio
In my case, the configuration of my service was overwriting the environment variable SBT_OPTSand JAVA_OPTS. I was able to set the limits by setting in my build.sbtthe following:
就我而言,我的服务配置覆盖了环境变量SBT_OPTS和JAVA_OPTS. 我能够通过设置build.sbt以下内容来设置限制:
javaOptions in Universal ++= Seq(
"-J-Xms1g",
"-J-Xmx2g")
Reference: https://www.scala-sbt.org/sbt-native-packager/archetypes/java_app/customize.html
参考:https: //www.scala-sbt.org/sbt-native-packager/archetypes/java_app/customize.html

