7

Apppreciate any expert here could advise for below JVM and swap space related queries. Thanks in advance

1) Am I right that Operating System will use swap space when OutOfMemory occured in JVM Java Heap, Perm Generation or Native Heap ? Or swap space is used for OutOfMemory in Native Heap ?

2) Am I right that Native heap size is not configurable at JVM, because OS will assign available RAM to JVM during runtime ?

3) How can we enable swap space for JVM, or swap space is enabled for all processes at Unix and Window level by default ?

4) Understand that swap space can affect application performance, is that best practice to disable swap space for JVM ? If not, what is the reason ?

5) How can we disable swap space and change the swap space size for particular JVM in both Unix and Window OS, or it is only configurable at OS level which is applied to all processes in the OS ?

Leanne
  • 127
  • 1
  • 2
  • 12
  • Most of those questions are not jvm-specific and probably should be asked on superuser. And you probably should ask those questions for one OS since configuration of these things is *very* different between OSes. – the8472 Apr 10 '17 at 12:10

2 Answers2

11

There are a lot of questions here... Operating systems indeed use swap space to create the so called virtual memory (which is obviously bigger then the RAM you might have). It is usually enabled by default, but you need to check.

You can not instruct the JVM to use only the physical RAM AFAIK, but that would be a limitation of the OS itself and not JVM (this should answer 5).

You can disable swap (again for the OS, not JVM), but that is a bad idea. There are multiple processes that run inside the operating system and they each need space to run into (that at some point in time might exceed your actual RAM). It indeed affects performance, but what is worse - some performance penalties (I assume the OS has many things to make this better for you) or the death of the application? (this should answer 4).

Regarding (2) there are two parameters that control how much heap you will have: Xmx - maximum heap that JVM process will use. And Xms - initial heap. Actually just recent there was a very good talk about this: here.

Community
  • 1
  • 1
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Eugene, Thanks for your explaination. – Leanne Apr 21 '17 at 06:20
  • I have accepted your explaination by clicking the up arrow sign, but forum does not show it because my reputation is below 15. Thanks – Leanne Apr 21 '17 at 13:41
  • 1
    Done. Thanks for above link – Leanne Apr 26 '17 at 07:19
  • Having a swap space for server-class JVM apps is actually a bad idea. See Java Performance: The Definitive Guide, p. 44: "And it works particularly badly for any kind of Java application (including a Swing-based GUI application running on your desktop) because of the Java heap" (bad for performance, especially in case of Full GCs) – Juraj Martinka May 23 '19 at 06:33
  • @JurajMartinka its not like you can control it. Java is just a normal process asking memory from the OS, which is _always_ virtual, including swap – Eugene May 23 '19 at 07:02
  • @Eugene that is correct; I was referring to the part of your answer saying: "You can disable swap (again for the OS, not JVM), but that is a bad idea." – Juraj Martinka May 23 '19 at 07:20
5

I think -Xmx and -Xms configure how much heap is available for the java process that is run inside the virtual machine. The virtual machine itself is a native process that requires additional heap for running the virtual machine itself. The JVM process can therefore consume more memory then that indicated by the -Xmx option.

Erik
  • 91
  • 1
  • 4