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时可以设置堆大小,但我希望通过我的服务器上的环境变量进行调整。
6 个解决方案
#1
57
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
#2
40
If you want any java
process, not just ant or Tomcat, to pick up options like -Xmx
use the environment variable _JAVA_OPTIONS
.
如果你想要任何java进程,而不仅仅是ant或Tomcat,来获取像-Xmx这样的选项,请使用环境变量_JAVA_OPTIONS。
In bash: export _JAVA_OPTIONS="-Xmx1g"
在bash中:export _JAVA_OPTIONS =“ - Xmx1g”
#3
11
Actually, there is a way to set global defaults for Sun's JVM via environment variables.
实际上,有一种方法可以通过环境变量为Sun的JVM设置全局默认值。
请参阅如何设置java系统属性,以便每当我启动JVM而不将其添加到命令行参数时它都有效。
#4
9
You can't do it using environment variables. It's done via "non standard" options. 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 -X了解详细信息。你正在寻找的选项是-Xmx和-Xms(这是“初始”堆大小,所以你可能正在寻找它。)
#5
3
I think your only option is to wrap java in a script that substitutes the environment variables into the command line
我认为您唯一的选择是将java包装在一个脚本中,该脚本将环境变量替换为命令行
#6
3
A couple of notes:
几个笔记:
-
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.
Apache ant对JAVA_OPTS一无所知,而Tomcat的启动脚本也是如此。对于Apache ant,使用ANT_OPTS来影响运行/ ant /的JVM的环境,但不会影响ant可能启动的内容。
-
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.
您可以设置的最大堆大小完全取决于环境:对于大多数32位系统,无论可用内存如何,您可以请求的最大堆空间量都在2GiB范围内。 64位系统上最大的堆“非常大”。此外,您实际上也受到物理内存的限制,因为堆由JVM管理,并且您不希望大量交换进入磁盘。
-
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.
对于服务器环境,您通常希望将-Xms和-Xmx设置为相同的值:这将以一定大小修复堆的大小,并且垃圾收集器的工作量较少,因为堆永远不必重新生成大小。
#1
57
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
#2
40
If you want any java
process, not just ant or Tomcat, to pick up options like -Xmx
use the environment variable _JAVA_OPTIONS
.
如果你想要任何java进程,而不仅仅是ant或Tomcat,来获取像-Xmx这样的选项,请使用环境变量_JAVA_OPTIONS。
In bash: export _JAVA_OPTIONS="-Xmx1g"
在bash中:export _JAVA_OPTIONS =“ - Xmx1g”
#3
11
Actually, there is a way to set global defaults for Sun's JVM via environment variables.
实际上,有一种方法可以通过环境变量为Sun的JVM设置全局默认值。
请参阅如何设置java系统属性,以便每当我启动JVM而不将其添加到命令行参数时它都有效。
#4
9
You can't do it using environment variables. It's done via "non standard" options. 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 -X了解详细信息。你正在寻找的选项是-Xmx和-Xms(这是“初始”堆大小,所以你可能正在寻找它。)
#5
3
I think your only option is to wrap java in a script that substitutes the environment variables into the command line
我认为您唯一的选择是将java包装在一个脚本中,该脚本将环境变量替换为命令行
#6
3
A couple of notes:
几个笔记:
-
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.
Apache ant对JAVA_OPTS一无所知,而Tomcat的启动脚本也是如此。对于Apache ant,使用ANT_OPTS来影响运行/ ant /的JVM的环境,但不会影响ant可能启动的内容。
-
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.
您可以设置的最大堆大小完全取决于环境:对于大多数32位系统,无论可用内存如何,您可以请求的最大堆空间量都在2GiB范围内。 64位系统上最大的堆“非常大”。此外,您实际上也受到物理内存的限制,因为堆由JVM管理,并且您不希望大量交换进入磁盘。
-
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.
对于服务器环境,您通常希望将-Xms和-Xmx设置为相同的值:这将以一定大小修复堆的大小,并且垃圾收集器的工作量较少,因为堆永远不必重新生成大小。