如何通过环境变量设置 Java 的最小和最大堆大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/417152/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:23:54  来源:igfitidea点击:

How do I set Java's min and max heap size through environment variables?

javaenvironment-variablesheap

提问by Jeff Thompson

How do I set Java's min and max heap size through environment variables?

如何通过环境变量设置 Java 的最小和最大堆大小?

I know that the heap sizes can be set when launching java, but I would like to have this adjusted through environment variables on my server.

我知道在启动 java 时可以设置堆大小,但我想通过我服务器上的环境变量来调整它。

采纳答案by Todd

You can't do it using environment variables directly. You need to use the set of "non standard" options that are passed to the java command. Run: java -X for details. The options you're looking for are -Xmx and -Xms (this is "initial" heap size, so probably what you're looking for.)

你不能直接使用环境变量来做到这一点。您需要使用传递给 java 命令的“非标准”选项集。运行:java -X 了解详情。您正在寻找的选项是 -Xmx 和 -Xms (这是“初始”堆大小,所以可能是您正在寻找的。)

Some products like Ant or Tomcat might come with a batch script that looks for the JAVA_OPTS environment variable, but it's not part of the Java runtime. If you are using one of those products, you may be able to set the variable like:

某些产品(如 Ant 或 Tomcat)可能带有一个批处理脚本,用于查找 JAVA_OPTS 环境变量,但它不是 Java 运行时的一部分。如果您正在使用其中一种产品,您可以将变量设置为:

set JAVA_OPTS="-Xms128m -Xmx256m"  

You can also take this approach with your own command line like:

您也可以使用自己的命令行来采用这种方法,例如:

set JAVA_OPTS="-Xms128m -Xmx256m"  
java ${JAVA_OPTS} MyClass

回答by a2800276

You can't do it using environment variables. It's done via "non standard" options. Run: java -Xfor details. The options you're looking for are -Xmxand -Xms(this is "initial" heap size, so probably what you're looking for.)

你不能使用环境变量来做到这一点。它是通过“非标准”选项完成的。运行:java -X查看详情。您正在寻找的选项是-Xmx-Xms(这是“初始”堆大小,所以可能是您正在寻找的。)

回答by frankodwyer

I think your only option is to wrap java in a script that substitutes the environment variables into the command line

我认为您唯一的选择是将 java 包装在一个脚本中,该脚本将环境变量替换为命令行

回答by Christopher Schultz

A couple of notes:

一些注意事项:

  1. Apache ant doesn't know anything about JAVA_OPTS, while Tomcat's startup scripts do. For Apache ant, use ANT_OPTS to affect the environment for the JVM that runs /ant/, but not for the things that ant might launch.

  2. The maximum heap size you can set depends entirely on the environment: for most 32-bit systems, the maximum amount of heap space you can request, regardless of available memory, is in the 2GiB range. The largest heap on a 64-bit system is "really big". Also, you are practically limited by physical memory as well, since the heap is managed by the JVM and you don't want a lot of swapping going on to the disk.

  3. For server environments, you typically want to set -Xms and -Xmx to the same value: this will fix the size of the heap at a certain size and the garbage collector has less work to do because the heap will never have to be re-sized.

  1. Apache ant 对 JAVA_OPTS 一无所知,而 Tomcat 的启动脚本却知道。对于 Apache ant,使用 ANT_OPTS 来影响运行 /ant/ 的 JVM 的环境,但不影响 ant 可能启动的东西。

  2. 您可以设置的最大堆大小完全取决于环境:对于大多数 32 位系统,无论可用内存如何,您可以请求的最大堆空间量都在 2GiB 范围内。64 位系统上的最大堆“非常大”。此外,您实际上也受到物理内存的限制,因为堆是由 JVM 管理的,您不希望对磁盘进行大量交换。

  3. 对于服务器环境,您通常希望将 -Xms 和 -Xmx 设置为相同的值:这会将堆的大小固定为特定大小,垃圾收集器可以做的工作更少,因为永远不必重新堆大小。

回答by Vadzim

Actually, there is a way to set global defaults for Sun's JVM via environment variables.

实际上,有一种方法可以通过环境变量为 Sun 的 JVM 设置全局默认值。

See How to set a java system property so that it is effective whenever I start JVM without adding it to the command line arguments.

请参阅如何设置 java 系统属性,以便它在我启动 JVM 时有效而不将其添加到命令行参数

回答by wmli

If you want any javaprocess, not just ant or Tomcat, to pick up options like -Xmxuse the environment variable _JAVA_OPTIONS.

如果您希望任何java进程(不仅仅是 ant 或 Tomcat)选择-Xmx使用环境变量等选项_JAVA_OPTIONS

In bash: export _JAVA_OPTIONS="-Xmx1g"

在 bash 中: export _JAVA_OPTIONS="-Xmx1g"

回答by Gauthier

You can use JAVA_TOOL_OPTIONS.

您可以使用JAVA_TOOL_OPTIONS.

Example:

例子:

export JAVA_TOOL_OPTIONS=-Xmx512m

It has been mentioned in some comments, and in another answer.

在一些评论和另一个答案中已经提到了它。

The OP's question is quite old, but as it is the first google result for the question, I thought i would add the answer here for clarity's sake.

OP的问题已经很老了,但由于它是该问题的第一个谷歌结果,我想为了清楚起见我会在这里添加答案。