As indicated in Understanding JVM Memory Allocation and Java Out of Memory: Heap Space, when heap space is allocated, JVM does not distinguish between physical and virtual memory. When the memory allocation of java objects and computations start happening, JVM starts to distinguish between virtual and physical memory. Given sufficient swap space (on a Linux machine), why does an out of memory error ever occur? Shouldn't the JVM simply use the swap space to complete the computations, albeit very slowly.
Eg: RAM: 14GB, swap space: 10GB
If a java application needs 20GB of space, couldn't it utilize the swap space (virtual memory) and complete running the application?
What is the interplay between heap space and virtual memory with respect to a java application?
Processes never know which parts of the memory they use are in virtual memory and which ones are in physical memory.
That is managed by the OS in a transparent way. Usually the most used or most recently used "blocks" (the correct term is "pages") of memory are kept in physical memory. A program just demands memory, the OS is the one that allocates it and decides where each page of it is located (virtual or physical).
To use some piece of memory, you must have the page where that piece is in physical memory. Moving a page of memory from virtual to physical is quite expensive, so you must try to avoid it. So, you usually specify that your heap is smaller than your physical memory, to allow the OS to try to keep all of it in physical memory (note that you should leave extra physical memory free for OS and other applications).
And for the question:
couldn't it utilize the additional 8GB from swap space (virtual memory) and complete running the application
You have told the JVM to use at most 12 GB of memory, and now you want the JVM to ignore your order and expand beyond those 12 GB? The JVM is a good guy and obeys you, it is you who should decide if you prefer the memory limit to ensure good performance or no heap limit and risk pagination (Of course, even with no explicit limit, there are limits due to the host machine OS and resources).
This link provides more information
Shouldn't the JVM simply use the swap space to complete the computations, albeit very slowly.
It is not just slowly, if you have spinning disk it can be more than 50,000x slower. When the GC is performed, it can scan GB/s of objects, but imagine instead it could only scan 100s of objects per second. This means instead of taking a few seconds, it would now take around a day. Note: while this is happening the machine becomes unusable. On Windows, you can't even kill the process and have to power cycle it to get it to stop.
Would I able to test this if I don't provide any -Xmx arguments and let the java application run as long as it takes
The defualt is no more than main memory. Even if you set the maximum heap size to larger than main memory, it doesn't mean it will use that much, you would have to set the minimum as well.
This might be an interesting test to see if your process complete successfully, given any amount of time but I would expect it to take many orders of maginutde longer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With