3

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?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Raghava
  • 947
  • 4
  • 15
  • 29
  • I think that the JVM puts boundary on memory usage allowed, it's not that it doesn't have enough memory, it's that the application has crossed its boundaries, I don't know though – niceman Jun 17 '16 at 20:55
  • 1
    Did you read Peter Lawrey's answer to the question you linked? He addresses most, if not all, of your questions. – Jonny Henly Jun 17 '16 at 20:57
  • Yes, I did. But I did not understand why a java process gives a OOM when there is sufficient virtual memory that it can use. – Raghava Jun 17 '16 at 21:49
  • Your question makes no sense. If you specify the option `-Xmx12g`, then **you** tell the JVM not to use more than 12GiB of memory, regardless of whether it is physical RAM or swap space. Why do you expect the JVM to counteract your commands? – Holger Jun 20 '16 at 14:28
  • Perhaps I should remove that line from the example. May be its a distraction to the actual question. I am only interested in the following: can a java application complete without giving an OOM if it needs more space than the physical memory but less than the combination of physical + virtual memory? If so, how can I achieve this? I understand that the java application is ignorant of where the allocated memory location is. – Raghava Jun 21 '16 at 03:02

2 Answers2

4

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

tkroman
  • 4,811
  • 1
  • 26
  • 46
SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • Thank you. How can I specify the following to the jvm: "use as much heap space as possible, and also the virtual memory to complete running the application" -- I don't mind if there is pagination involved. Goal is to complete running the application without any worry for time taken. Application/RAM stats are mentioned in the example of the post. – Raghava Jun 17 '16 at 21:47
  • From the link I provided: `If you do not set an initial or maximum heap size, the GC expands and shrinks the heap as required. However, if you fix the heap size by using the -Xms and -Xmx options, the GC does not expand or shrink the Java™ heap`. Again, **processes only ask for memory**, the OS decides where to allocate it. – SJuan76 Jun 18 '16 at 17:37
  • Assuming the same holds true for Oracle Java SDK as well, is the following correct: a) if -Xms and -Xmx options are not provided, no default value would be assumed and b) we can expect no OOM error if there is sufficient swap space and the java application requires more space than physical memory available? – Raghava Jun 18 '16 at 22:36
  • @Raghava you don't have to provide -Xmx now, it defaults to 1/4 of main memory. – Peter Lawrey Jun 20 '16 at 00:16
3

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thank you. I am interested in testing how long a java application if it needs more space than the available physical memory. 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 (and hope that it uses virtual memory)? – Raghava Jun 20 '16 at 04:09
  • Thank you. I will test it with both -Xmx option and not specifying any heap size. – Raghava Jun 21 '16 at 02:56