1

We have a Linux server with 32 GB RAM. So the Default Heap size allocated in the server is 1/4th which is 8 GB. java -XX:+PrintFlagsFinal -version | grep HeapSize

Now, we have 3 java processes running in the server.

The maximum heap size(-Xmx) required for
Process 1 = 4 GB
Process 2 = 4 GB
Process 3 = 2 GB

So the sum of max heap size becomes 10 GB which is greater than Server's max heap size 8 GB.

Is it fine to allocate max heap size like this? Will this lead to any issue in future?

Roopashree
  • 140
  • 8
  • 1
    A server doesn't have "heap". It has memory. – Stephen C May 10 '21 at 07:49
  • Oh Okay. This is what I see in Linux server ```uintx MaxHeapSize := 8311013376``` when I execute ```java -XX:+PrintFlagsFinal -version | grep HeapSize```. Hence, I thought that is the max heap size allowed in the server. Please correct me, if I'm wrong. – Roopashree May 10 '21 at 08:06
  • 1
    It is reporting the actual max heap size for the JVM. See https://stackoverflow.com/questions/26631498/understanding-java-heap. (And even if it was the largest allowed heap size, that is still a JVM heap size ... not anything to do with the hardware or the OS. The OS doesn't have any idea what a process does with its memory.) – Stephen C May 10 '21 at 08:37
  • Got it @Stephan C ! Thank you ! :) Now I understand. – Roopashree May 10 '21 at 08:52

3 Answers3

2

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

First of: the Ram a server has is called memory not heap size ;) And the thing you define with -Xmx is how much memory a application can use. This means that you will use 10GB of ram of 32GB of ram for your applications which isn't nearly touching the limits of your server.

Edit: But it still could lead to problems if you would allocate more than 32GB like stephen wrote.

Waterdev
  • 54
  • 3
1

You should not see any problem.

As you mention, the 'default' max heap size is 8 GB. That is an upper bound and does not mean that 8 GB of memory are allocated or used.

You specify that two processes need 4 GB and one of them needs 2 GB max heap. Feel free to configure them, they very likely go into three separate invocations of Java. That means each of the JVMs you have at runtime can get the amount of heap they need - and it is way below the 32 GB of memory.

But do not start to wonder when you see the virtual memory size of the java processes to be bigger than the max heap size. Heap is only one way for java to use memory, and there are other regions as well.

I still believe you are safe unless you run a lot of other stuff on the same server that you did not tell us about.

Queeg
  • 7,748
  • 1
  • 16
  • 42