Does JVM creates its own swap memory or it takes the OS swap memory? What if OS doesn't have any swap memory, will JVM stick to RAM only?
-
1I think this should answer your question. [JVM Swap OS query](https://stackoverflow.com/questions/43320272/java-virtual-machine-and-swap-space) – Neil D'souza May 22 '19 at 05:58
3 Answers
The JVM behaves just like any other process running on the kernel. When the JVM needs heap space, it will request memory via the malloc library call, which in turn uses a system call (brk, sbrk or mmap) to allocate space using the OS virtual memory system. Whether this involves paging to swap is not relevant to the JVM, it's just asking for space and leaves the details up to the OS VM sub-system.
From a performance perspective, it is important to bear this in mind when configuring your JVM. Although you can make the heap bigger than the physical memory of your machine it will deliver worse performance than if the heap is less than physical memory. This is a result of the paging activity that must occur once the heap exceeds physical memory.
As an aside, Azul (who I work for) have a JVM called Zing that does things slightly differently. Rather than using the Linux VM sub-system, we have our own kernel-level software (called Zing System Tools or ZST). This allows us to handle virtual memory in a way that is specific to the JVM, rather than in a general purpose way as Linux does. Memory can be returned from a JVM to ZST when not being used, cache lines can be more optimized and we also have the ability to allow a JVM to exceed the configured heap size to prevent an OOM error.
-
well from java-12, memory can be configured to be returned also to the OS on GC calls on specific intervals IIRC; otherwise very good answer – Eugene May 22 '19 at 12:16
-
Except that the JVM doesn't usually use malloc for allocating heap memory. Instead, it uses mmap system call to reserve and later allocate larger blocks of memory and then manages the memory internally - check https://softwareengineering.stackexchange.com/questions/324192/how-do-virtual-machines-allocate-memory, https://www.quora.com/Does-Javas-new-use-malloc-internally, https://stackoverflow.com/questions/35502348/how-does-a-jvm-process-allocate-its-memory and http://hiroshiyamauchi.blogspot.com/2009/12/jvm-process-memory.html – Juraj Martinka May 23 '19 at 06:35
-
Since Java 12, G1 GC is able to release unused memory to OS during Concurrent cycle (https://openjdk.java.net/jeps/346). It was capable to do that before, but only during Full GC (making it much less useful). Shenandoah GC is also able to return memory to operating system. – Juraj Martinka May 23 '19 at 06:38
Virtual memory is a trick used by many operating systems to for all practical purposes behave like they have more physical RAM than they really have. This is usually invisible to normal programs and the operating system handles everything.
HotSpot-based Java implementations are normal programs but can grow very big. It is therefore important that the JVM is configured correctly so it does not use too much memory and cause swapping. The defaults are usually reasonable.
So, in short. The jvm does not concern itself with swapping - this is purely an operating system task.

- 73,784
- 33
- 194
- 347
JVM wont create its own swap memory in disk, it uses OS swap just like any other app. If OS doesnt have SWAP configured, it will stick to RAM.

- 13
- 3