What is the solution?

To overcome this issue, you have several possibilities. First and often the easiest workaround is to increase swap space. The means for this are platform specific, for example in Linux you can achieve with the following example sequence of commands, which create and attach a new swapfile sized at 640MB:

Now, you should recall that due to garbage collection sweeping the memory content, swapping is undesirable for Java processes in general. Running garbage collection algorithms on swapped allocations can increase the length of GC pauses by several orders of magnitude, so you should think twice before jumping to the easy solution bandwagon.

If your application is deployed next to a “noisy neighbor” with whom the JVM needs to compete for resources, you should isolate the services to separate (virtual) machines.

And in many cases, your only truly viable alternative is to either upgrade the machine to contain more memory or optimize the application to reduce its memory footprint. When you turn to the optimization path, a good way to start is by using memory dump analyzers to detect large allocations in memory.