-1

I have a machine with 10GB of RAM. I am running 6 java processes with the -Xmx option set to 2GB. The probability of all 6 processes running simultaneously and consuming the entire 2GB memory is very very low. But I still want to understand this worst case scenario.

What happens when all 6 processes consume a little less than 2GB memory at the same instant such that the JVM does not start garbage collection yet the processes are holding that much memory and the sum of the memory consumed by these 6 processes exceeds the available RAM?

Will this crash the server?
OR
Will it slow down the processing?

ryk
  • 133
  • 2
  • 9

2 Answers2

2

You should expect each JVM could use more than 2 GB. This is because the heap is just one memory region, you also have

  • shared libraries
  • thread stacks
  • direct memory
  • native memory use by shared libraries
  • perm gen.

This means that setting a maximum heap of 2 GB doesn't mean your process maximum 2 GB.

Your processes should perform well until they get the point where you have swapped some of the heap and a GC is performed. A GC assumes random access to the whole heap and at this point, your system could start swapping like mad. If you have a SSD for swap your system is likely to stop, or almost stop for very long periods of time. If you have Windows (which I have found is worse than Linux in this regard) and a HDD, you might not get control of the machine back and have to power cycle it.

I would suggest either reducing the heap to say 1.5 GB at most, or buying more memory. You can get 8 GB for about $100.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I owe you a beer, I didn't know the JVM returned memory back. Here's an explanation: http://stackoverflow.com/questions/675589/jvm-sending-back-memory-to-os – Augusto Dec 09 '13 at 11:03
1

Your machine will start swapping. As long as each java process uses only a small part of the memory it has allocated, you won't notice the effect, but if they all garbage collect at the same time, accessing all of their memory, your hard disk will have 100% utilization and the machine will "feel" very, very slow.

Guntram Blohm
  • 9,667
  • 2
  • 24
  • 31
  • On top of that if a process peaks out and takes 2GB of memory, the JVM won't return any of that memory back to the OS, even if the process goes back to use 200MB of ram from than moment onwards. This means that long lived application have potentially quite a high change of using 2GB each. – Augusto Dec 09 '13 at 09:59
  • 1
    @Augusto It is not that the JVM cannot return memory to the OS, but in my experience it rarely does when you might expect it to. – Peter Lawrey Dec 09 '13 at 10:37