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


本文翻译自:What are the -Xms and -Xmx parameters when starting JVM?

Please explain the use of Xms and Xmx parameters in JVMs. 请说明XmsXmx参数的用法。 What are the default values for them? 它们的默认值是多少?


#1楼

参考:https://stackoom.com/question/zwYB/启动JVM时-Xms和-Xmx参数是什么


#2楼

The flag Xmx specifies the maximum memory allocation pool for a Java virtual machine (JVM), while Xms specifies the initial memory allocation pool. 标志Xmx指定Java虚拟机(JVM)的最大内存分配池,而Xms指定初始内存分配池。

This means that your JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of memory. 这意味着您的JVM将以Xms的内存量启动,并且最多可以使用Xmx的内存量。 For example, starting a JVM like below will start it with 256 MB of memory and will allow the process to use up to 2048 MB of memory: 例如,启动如下所示的JVM将以256 MB的内存启动它,并将允许该进程使用最多2048 MB的内存:

1
java -Xms256m -Xmx2048m

The memory flag can also be specified in different sizes, such as kilobytes, megabytes, and so on. 也可以以不同的大小指定内存标志,例如千字节,兆字节等。

1
2
3
-Xmx1024k
-Xmx512m
-Xmx8g

The Xms flag has no default value, and Xmx typically has a default value of 256 MB. Xms标志没有默认值,而Xmx通常具有256 MB的默认值。 A common use for these flags is when you encounter a java.lang.OutOfMemoryError . 这些标志的常见用法是当您遇到java.lang.OutOfMemoryError

When using these settings, keep in mind that these settings are for the JVM's heap , and that the JVM can/will use more memory than just the size allocated to the heap. 使用这些设置时,请记住,这些设置用于JVM的 ,并且JVM可以/将使用的内存比分配给堆的大小更多。 From Oracle's documentation : 从Oracle文档中 :

Note that the JVM uses more memory than just the heap. 请注意,JVM使用的内存不仅仅是堆。 For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures. 例如,Java方法,线程堆栈和本机句柄与堆以及JVM内部数据结构分开分配在内存中。


#3楼

Run the command java -X and you will get a list of all -X options: 运行命令java -X ,您将获得所有-X选项的列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

The -X options are non-standard and subject to change without notice. -X选项是非标准的,如有更改,恕不另行通知。

I hope this will help you understand Xms , Xmx as well as many other things that matters the most. 希望这可以帮助您了解XmsXmx以及其他最重要的事情。 :) :)


#4楼

The question itself has already been addressed above. 上面已经解决了这个问题。 Just adding part of the default values. 仅添加部分默认值。

As per http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html 按照http://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html

The default value of Xmx will depend on platform and amount of memory available in the system. Xmx的默认值将取决于平台和系统中可用的内存量。


#5楼

You can specify it in your IDE. 您可以在IDE中指定它。 For example, for Eclipse in Run ConfigurationsVM arguments . 例如,对于Eclipse中的运行配置VM参数 You can enter -Xmx800m -Xms500m as 您可以输入-Xmx800m -Xms500m作为

在此处输入图片说明


#6楼

The JVM memory consists of the following segments: JVM内存包括以下部分:

  1. Heap Memory, which is the storage for Java objects 堆内存,这是Java对象的存储
  2. Non-Heap Memory, which is used by Java to store loaded classes and other meta-data 非堆内存,Java用来存储已加载的类和其他元数据
  3. JVM code itself, JVM internal structures, loaded profiler agent code and data, etc. JVM代码本身,JVM内部结构,已加载的探查器代理程序代码和数据等。

The JVM has a heap that is the runtime data area from which memory for all class instances and arrays are allocated. JVM具有一个堆,该堆是运行时数据区域,从中分配了所有类实例和数组的内存。 It is created at the JVM start-up. 它是在JVM启动时创建的。

The heap size may be configured with the following VM options: 可以使用以下VM选项配置堆大小:

1
2
-Xmx<size> - to set the maximum Java heap size
-Xms<size> - to set the initial Java heap size

By default, the maximum heap size is 64 MB. 默认情况下,最大堆大小为64 MB。

Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector. 对象的堆内存被称为垃圾收集器的自动内存管理系统回收。 The heap may be of a fixed size or may be expanded and shrunk, depending on the garbage collector's strategy. 根据垃圾收集器的策略,堆的大小可以是固定的,也可以是缩小的。

The following command start JVM with 256MB of memory, and allow the Java process to use up to 4G (4096MB) of memory. 以下命令以256MB的内存启动JVM,并允许Java进程使用多达4G(4096MB)的内存。

1
java -Xms256m -Xmx4g