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

2022-01-15 00:00:00 environment-variables java heap-memory

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

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 命令的一组非标准"选项.运行:java -X 了解详情.您正在寻找的选项是 -Xmx 和 -Xms(这是初始"堆大小,因此可能是您正在寻找的.)

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.)

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

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:

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

相关文章