5

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.

Adam_G
  • 7,337
  • 20
  • 86
  • 148
  • 3
    What happened when you tried? – Kevin Workman Feb 05 '14 at 17:01
  • 1
    Try it and see. OS's provide [swap space](http://en.wikipedia.org/wiki/Paging) that make the total amount of virtual memory available larger than the amount of physical memory present, as long as not all the memory is needed simultaneously (pages can be "swapped out"). – TypeIA Feb 05 '14 at 17:02
  • 5
    @RC : why not? do you still believe 640kB is enough? – njzk2 Feb 05 '14 at 17:04
  • @RC. - I know it was kind of overkill. But I do huge data processing, and don't always have access to my school's servers. EDIT: I was saving up for over a year, and since Apple kept delaying the release date of the 2013 MBP, I just kept saving more... :-) – Adam_G Feb 05 '14 at 17:04
  • 2
    @njzk2 Hey, I used to program an Apple II+ with 56KB of RAM! I also used to walk five miles to school, uphill both ways ... – ajb Feb 05 '14 at 17:06
  • @njzk2 no but 16g is some serious RAM usage IMHO –  Feb 05 '14 at 17:06
  • Here is your answer: [Link to Stack Overflow Question][1] [1]: http://stackoverflow.com/questions/2457514/understanding-max-jvm-heap-size-32bit-vs-64bit – ThePerson Feb 05 '14 at 17:17
  • This link about [maximum heap size](http://javarevisited.blogspot.com/2013/04/what-is-maximum-heap-size-for-32-bit-64-JVM-Java-memory.html) can be useful for you. It depends on your OS. – Juan Pedro López Frechoso Feb 05 '14 at 17:16

2 Answers2

8

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.

Eduardo Briguenti Vieira
  • 4,351
  • 3
  • 37
  • 49
1

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

theo231022
  • 329
  • 2
  • 12