How Much Memory Can I Allocate?

狂风中的少年 提交于 2021-02-07 14:21:59

问题


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?

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.

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


回答1:


The size the heap memory of the virtual machine is controlled by the options - Xms and - 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.

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

It is common to use the same value for Xms and 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.


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)

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




回答2:


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



来源:https://stackoverflow.com/questions/21583575/how-much-memory-can-i-allocate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!