1. Overview
1.概述
In this tutorial, we’ll learn the cause of “Could not reserve enough space for object heap” error, while going through some possible scenarios.
在本教程中,我们将学习“无法为对象堆保留足够的空间”错误的原因,同时通过一些可能的情况。
2. Symptoms
2.症状
“Could not reserve enough space for object heap” is a specific JVM error that is raised when Java process cannot create the virtual machine due to memory constraints encountered on the running system:
“无法为对象堆保留足够的空间 “是一个特定的JVM错误,当Java进程无法创建虚拟机时,由于在运行系统中遇到的内存限制而引发的错误:虚拟机。
java -Xms4G -Xmx4G -jar HelloWorld.jar
Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Generally, there are two possible scenarios when we encounter the error.
Firstly, when we lunch a Java process with max heap size limit parameter (-Xmx) and the value is more than what the process can have on the operating system.
一般来说,当我们遇到这个错误时,有两种可能的情况。
首先,当我们午餐一个Java进程的最大堆大小限制参数(-Xmx),并且该值是超过该进程在操作系统上所能拥有的。
The heap size limit varies based on several constraints:
堆的大小限制基于几个约束条件而变化。
- hardware architecture (32/64 bit)
- JVM bit version (32/64 bit)
- the operating system that we use
Secondly, when the Java process is not able to reserve the specified amount of memory due to other applications that run on the same system and consume memory.
其次,当Java进程无法保留指定的内存量时,由于其他应用程序在同一系统上运行并消耗内存。
3. Heap Size
3.堆积大小
Java heap space is the memory allocation pool for the runtime Java program, managed by the JVM itself. By default, the allocation pool is restricted to the initial and maximum size. To learn more about Heap Space in Java, have a look at this article here.
Java 堆空间是运行中的Java程序的内存分配池,由JVM自己管理。默认情况下,分配池被限制在初始和最大大小。要了解有关Java中堆空间的更多信息,请看这里的文章。
Let’s see what the maximum heap size is in different environments and how we can set the limits.
让我们看看在不同的环境中最大的堆尺寸是多少,以及我们如何设置限制。
3.1. Max Heap Size
3.1.最大堆尺寸
The maximum theoretical heap limit for the 32-bit and 64-bit JVM is easy to determine by looking at the available memory space, 2^32 (4 GB) for 32-bit JVM and 2^64 (16 Exabytes) for 64-bit JVM.
32位和64位JVM的最大理论堆限制很容易通过查看可用内存空间来确定,32位JVM为2^32(4GB),64位JVM为2^64(16Exabytes)。
In practice, due to various constraints, the limit can be much lower and varies given the operating system. For example, on 32-bit Windows systems the maximum heap size range is between 1.4 GB to 1.6 GB. In contrast, on 32-bit Linux systems, the maximum heap size can stretch up to 3 GB.
在实践中,由于各种限制,该限制可能会低得多,并且根据操作系统的不同而不同。例如,在32位Windows系统中,最大的堆大小范围在1.4GB到1.6GB之间。相比之下,在32位的Linux系统上,最大的堆大小可以扩展到3GB。
For this reason, if the application requires a large heap we should use the 64-bit JVM. However, with a large heap, the garbage collector will have more work to do, so it’s important to find a good balance between heap size and performance.
出于这个原因,如果应用程序需要一个大堆,我们应该使用64位JVM。然而,有了大堆,垃圾收集器将有更多的工作要做,所以在堆的大小和性能之间找到一个良好的平衡点是很重要的。
3.2. How To Control Heap Size Limits?
3.2.如何控制堆的大小限制?
We have two options to control the heap size limits of a JVM.
我们有两个选项来控制JVM的堆大小限制。
First, by using Java command line parameters at each JVM initialization:
首先,通过在每次JVM初始化时使用Java命令行参数。
-Xms<size> Sets initial Java heap size. This value must be a multiple of 1024 and greater than 1 MB.
-Xmx<size> Sets maximum Java heap size. This value must be a multiple of 1024 and greater than 2 MB.
-Xmn<size> Sets the initial and maximum size (in bytes) of the heap for the young generation.
For the size value, we may append letter k or K, m or M and g or G to indicate kilobytes, megabytes, and gigabytes respectively. If no letter is specified, the default unit (byte), is used.
对于大小值,我们可以附加字母k或K,m或M和g或G来分别表示千字节、兆字节和吉字节。如果没有指定字母,将使用默认单位(字节)。
-Xmn2g
-Xmn2048m
-Xmn2097152k
-Xmn2147483648
Secondly, by using environment variable JAVA_OPTS to configure above Java command line parameters globally. Due to this, each JVM initialization on the system will automatically use the configurations set in the environment variable.
其次,通过使用环境变量JAVA_OPTS来全局配置上述Java命令行参数。由于这个原因,系统上的每个JVM初始化都会自动使用环境变量中设置的配置。
JAVA_OPTS="-Xms256m -Xmx512m"
For more information, check out our comprehensive JVM Parameters guide.
欲了解更多信息,请查看我们全面的JVM参数指南。
4. Conclusion
4.总结
In this tutorial, we discussed two possible scenarios when JVM is not able to reserve enough space for object heap. We also learned how to control the heap size limits in order to mitigate this error.
在本教程中,我们讨论了当JVM无法为对象堆保留足够空间时的两种可能情况。我们还学习了如何控制堆的大小限制,以减轻这种错误。
Next, learn more about potential memory issues at runtime and how to identify them.
接下来,了解更多关于运行时的潜在内存问题以及如何识别这些问题。