Is it fine to allocate max heap size like this? Will this lead to any issue in future?
If 8GB was the server's RAM, that could well lead to problems; see below.
However the 8GB that is being reported by
$ java -XX:+PrintFlagsFinal -version | grep HeapSize
is the default MaxHeapSize
for a JVM for your server hardware1; i.e. the heap size you will get if you don't provide any heap size parameters. It is NOT the largest possible Java heap size. If you want to set the actual MaxHeapSize
, use -Xmx...
.
The actual RAM available will be 32GB ... less the RAM that is used by the OS kernel and other processes. 10GB of Java heaps (plus other non-heap memory) should be fine.
Provided that you have enough swap space, a Linux system can run with the aggregate of all processes virtual memory requirements exceeding the physical RAM on the system.
However, if you commit (significantly) more virtual memory than you have physical RAM:
Performance will suffer when a process attempts to access a VM page that is currently swapped out.
If there is too much swapping, Linux will take steps to prevent the system from thrashing. The kernel's OOM killer process will try to identify the cause of the heavy memory demand .... and KILL -9
it.
When a JVM does a full garbage collection, it is liable to repeatedly access most of its pages in (essentially) random order. This is liable to generate a lot of paging ... if you have overcommitted memory.
Unfortunately, the OOM killer has no notion of the importance of the various processes it assesses as candidates for killing. So, if you are lucky(!) it will kill the Java process that is causing the most problems. If you are unlucky, it might kill a process that is more critical; e.g. your database process, the ssh daemon, etc. In theory, you can protect a process against being killed. In practice it is awkward to do ... and you may end up in a worse situation by protecting things that ought to be killed.
1 - On current versions of Java, the default MaxHeapSize
is 1/4 of the total RAM ... as reported by the OS. You have 32GB of RAM, ergo the default MaxHeapSize
is 8GB.