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.

8
  • 3
    What happened when you tried? Feb 5, 2014 at 17:01
  • 1
    Try it and see. OS's provide swap space 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 5, 2014 at 17:02
  • 5
    @RC : why not? do you still believe 640kB is enough?
    – njzk2
    Feb 5, 2014 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 5, 2014 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 5, 2014 at 17:06

2 Answers 2

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.

0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.