协慌网

登录 贡献 社区

启动 JVM 时 - Xms 和 - Xmx 参数是什么?

请解释在 JVM 中使用XmsXmx参数。它们的默认值是什么?

答案

标志Xmx指定 Java 虚拟机(JVM)的最大内存分配池,而Xms指定初始内存分配池。

这意味着您的 JVM 将以Xms的内存量启动,并且能够使用最大Xmx内存量。例如,启动如下所示的 JVM 将启动 256 MB 内存,并允许进程使用最多 2048 MB 的内存:

java -Xms256m -Xmx2048m

内存标志也可以指定不同的大小,例如千字节,兆字节等。

-Xmx1024k
-Xmx512m
-Xmx8g

Xms标志没有默认值, Xmx的默认值通常为 256 MB。当遇到java.lang.OutOfMemoryError时,这些标志的常见用法是。

使用这些设置时,请记住这些设置是针对 JVM 的堆的 ,并且 JVM 可以 / 将使用的内存多于分配给堆的大小。来自Oracle 的文档

请注意,JVM 使用的内存多于堆。例如,Java 方法,线程堆栈和本机句柄分配在与堆不同的内存中,以及 JVM 内部数据结构中。

运行命令java -X ,您将获得所有-X选项的列表:

C:\Users\Admin>java -X
-Xmixed           mixed mode execution (default)
-Xint             interpreted mode execution only
-Xbootclasspath:<directories and zip/jar files separated by ;>
                      set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by ;>
                      append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by ;>
                      prepend in front of bootstrap class path
-Xdiag            show additional diagnostic messages
-Xnoclassgc       disable class garbage collection
-Xincgc           enable incremental garbage collection
-Xloggc:<file>    log GC status to a file with time stamps
-Xbatch           disable background compilation
-Xms<size>        set initial Java heap size.........................
-Xmx<size>        set maximum Java heap size.........................
-Xss<size>        set java thread stack size
-Xprof            output cpu profiling data
-Xfuture          enable strictest checks, anticipating future default
-Xrs              reduce use of OS signals by Java/VM (see documentation)
-Xcheck:jni       perform additional checks for JNI functions
-Xshare:off       do not attempt to use shared class data
-Xshare:auto      use shared class data if possible (default)
-Xshare:on        require using shared class data, otherwise fail.
-XshowSettings    show all settings and continue
-XshowSettings:all         show all settings and continue
-XshowSettings:vm          show all vm related settings and continue
-XshowSettings:properties  show all property settings and continue
-XshowSettings:locale      show all locale related settings and continue

-X 选项是非标准的,如有更改,恕不另行通知。

我希望这将有助于您了解XmsXmx以及其他许多重要事项。 :)

您可以在 IDE 中指定它。例如,对于运行配置中的 Eclipse→ VM 参数 。您可以输入-Xmx800m -Xms500m as

在此输入图像描述