java 如果您指定的最大堆大小大于可用 RAM 会发生什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3648454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What happens if you specify max heap size greater than available RAM
提问by illcar
Asked in an interview. What happens if you specify max heap size (Xmx) greater than available RAM? I also wonder what happens if you specify min heap size (Xms) greater than available RAM?
在采访中被问到。如果您指定的最大堆大小 (Xmx) 大于可用 RAM,会发生什么情况?我还想知道如果您指定的最小堆大小 (Xms) 大于可用 RAM,会发生什么?
采纳答案by Jay Askren
The easiest way to find out is try it and see.
找出答案的最简单方法是尝试并查看。
Edit:There are actually at least two answers to the question. Probably on a 64 bit system, as was mentioned, your app could grow and grow in memory usage and start thrashing. On a 32 bit system the story is a little different because the os is not able to give you that much heap space. For instance, if I run an app on Windows XP with 32 bit java with the command line option -Xmx2000m it will die with a message similar to the following:
编辑:这个问题实际上至少有两个答案。可能在 64 位系统上,正如前面提到的,您的应用程序可能会增加内存使用量并开始抖动。在 32 位系统上,情况略有不同,因为操作系统无法为您提供那么多堆空间。例如,如果我使用命令行选项 -Xmx2000m 在带有 32 位 java 的 Windows XP 上运行应用程序,它将死,并显示类似于以下的消息:
Invalid maximum heap size: -Xmx2000m
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.
无效的最大堆大小:-Xmx2000m
指定的大小超过了可表示的最大大小。
无法创建Java虚拟机。
In Linux with 32 bit java, I get the following with -Xmx3000m:
在具有 32 位 java 的 Linux 中,我使用 -Xmx3000m 得到以下结果:
Could not create the Java virtual machine.
Error occurred during initialization of VM
Could not reserve enough space for object heap
无法创建Java虚拟机。
虚拟机初始化时出错
无法为对象堆保留足够的空间
In Linux with 32 bit java, I get the following with -Xmx6000m
在具有 32 位 java 的 Linux 中,我使用 -Xmx6000m 得到以下结果
Invalid maximum heap size: -Xmx6000m
The specified size exceeds the maximum representable size.
Could not create the Java virtual machine.
无效的最大堆大小:-Xmx6000m
指定的大小超过了可表示的最大大小。
无法创建Java虚拟机。
Trying this with 64 bit Java, the JVM does allow you to allocate more memory than there is physical RAM, though if you ask for an extremely large amount of memory, the jvm will again fail with an error.
使用 64 位 Java 尝试此操作时,JVM 确实允许您分配比物理 RAM 多的内存,但是如果您要求非常大的内存量,jvm 将再次失败并出现错误。
回答by DigitalRoss
Nothing Dramatic
没什么戏剧性的
Although it can happen with certain low-end embedded systems, these days it would be quite rare to see a non-virtual Java environment even in embedded and impossible on a desktop or server.
尽管某些低端嵌入式系统可能会发生这种情况,但如今,即使在嵌入式系统中也很少见到非虚拟 Java 环境,而在台式机或服务器上则是不可能的。
So, nothing dramatic would happen, but once you use up available RAM, allocating additional (virtual) memory would just unnecessarily delay reclamation (garbage collection) and cause the program to start paging.
所以,不会发生什么戏剧性的事情,但是一旦你用完可用 RAM,分配额外的(虚拟)内存只会不必要地延迟回收(垃圾收集)并导致程序开始分页。
If severe, this condition is called "thrashing" and it is not a good thing. Stuff would run slowly.
如果严重,这种情况称为“颠簸”,可不是什么好事。东西会运行得很慢。
回答by StevenWernerCS
Only if your -Xms (minimum)is larger than available memory will you get an immediate failure on initialization of the JVM
只有当您的-Xms(最小值)大于可用内存时,您才会在 JVM 初始化时立即失败
$>java -Xms100g #JVM fails to start
Error occurred during initialization of VM Could not
reserve enough space for object heap
If your -Xmx (maximum)is larger than available memory your JVM does initialize since you are not using memory yet
如果您的 - Xmx(最大值)大于可用内存,则您的 JVM 会初始化,因为您尚未使用内存
$>java -Xmx100g #JVM starts up fine
Usage: java [-options] class [args...]
...
If your -Xmx (maximum) is larger than the available memory (total memory to include any virtual memory) you willget a runtime failure if and only ifyour JVM processes actually tries to use more memory than the machine has.
如果您的 -Xmx(最大值)大于可用内存(包括任何虚拟内存的总内存),当且仅当您的 JVM 进程实际尝试使用比机器更多的内存时,您才会遇到运行时故障。
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00007f5feb100000, 927465472, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 927465472 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /some/file/path/hs_err_pid25.log
It wont 'thrash'until it nears your -Xmx limit, but if that limit is above your available memory you will get the above memory allocation error and your program will terminate before thrashing is even considered. (And that is very dramatic!)
在接近您的 -Xmx 限制之前它不会“颠簸”,但是如果该限制高于您的可用内存,您将收到上述内存分配错误,并且您的程序将在甚至考虑颠簸之前终止。(这是非常戏剧性的!)