问题
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