Java 我可以分配多少内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21583575/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:30:39  来源:igfitidea点击:

How Much Memory Can I Allocate?

javamemoryram

提问by Adam_G

If I have 16 GB of RAM on my machine, how much can I allocate to a java command line program I'm executing? I assume java -Xmx 16g...will crash my system?

如果我的机器上有 16 GB 的 RAM,我可以为我正在执行的 java 命令行程序分配多少内存?我假设java -Xmx 16g...会崩溃我的系统?

EDIT:
In light of the comments, I tried java -Xmx16g..., and it did not crash my machine. The program still ran out of memory. I tried java -Xmx32g..., which did crash my machine.

编辑:
根据评论,我尝试了java -Xmx16g...,它没有使我的机器崩溃。该程序仍然内存不足。我试过了java -Xmx32g...,这确实使我的机器崩溃了。

From the comments below (which have been really enlightening), I guess I just need to keep playing around with the allocations.

从下面的评论(确实很有启发性),我想我只需要继续玩分配。

回答by edubriguenti

The size the heap memory of the virtual machine is controlled by the options - Xmsand - Xmx.

虚拟机的堆内存大小由选项-Xms-Xmx 控制

The first specifies the initial heap size and the second maximum size.

第一个指定初始堆大小和第二个最大大小。

The allocation is done within the JVM itself,and not on the OS. As more memory is needed, the JVM allocates a block of memory until the Xmx is reached.

分配是在JVM 本身内完成的,而不是在操作系统上完成。随着需要更多内存,JVM 会分配一块内存,直到达到 Xmx。

If you need more than that, an OutOfMemoryError is thrown.

如果您需要更多,则会抛出 OutOfMemoryError 。

It is common to use the same value for Xmsand Xmx, causing the VM to allocate memory in the operating system only at the beginning of an execution, not depending on the OS specific behavior.

XmsXmx使用相同的值是很常见的,这会导致 VM 仅在执行开始时在操作系统中分配内存,而不取决于操作系统的特定行为



In your case, since you set 32g as your Xmx and your system crashed, it seems you don't have this memory (swap + memory ram)

在您的情况下,由于您将 32g 设置为 Xmx 并且您的系统崩溃了,您似乎没有这个内存(交换 + 内存 ram)

If you manually change the virtual memory sizeof your system, the configuration will work.

如果您手动更改系统的虚拟内存大小,配置将起作用。

回答by theo231022

It is not possible for your OS to allocate more than 16GB and therefore your system crash. Apart from XMX and XMS that is stated in another answer there is also an -XX:+AggressiveHeap option which inspects the machine resources (size of memory and number of processors) and attempts to set various parameters to be optimal for long-running, memory allocation-intensive jobs

您的操作系统不可能分配超过 16GB 的空间,因此您的系统崩溃。除了在另一个答案中陈述的 XMX 和 XMS 之外,还有一个 -XX:+AggressiveHeap 选项,它检查机器资源(内存大小和处理器数量)并尝试设置各种参数以优化长时间运行,内存分配密集型工作