Java memory swap space

java.lang.OutOfMemoryError:
Out of swap space?

Java applications are given limited amount of memory during the startup. This limit is specified via the -Xmx and other similar startup parameters. In situations where the total memory requested by the JVM is larger than the available physical memory, operating system starts swapping out the content from memory to hard drive.

java.lang.outofmemoryerror swap

The java.lang.OutOfMemoryError: Out of swap space? error indicates that the swap space is also exhausted and the new attempted allocation fails due to the lack of both physical memory and swap space.

What is causing it?

The java.lang.OutOfmemoryError: Out of swap space? is thrown by JVM when an allocation request for bytes from the native heap fails and the native heap is close to exhaustion. The message indicates the size (in bytes) of the allocation which failed and the reason for the memory request.

The problem occurs in situations where the Java processes have started swapping, which, recalling that Java is a garbage collected language is already not a good situation. Modern GC algorithms do a good job, but when faced with latency issues caused by swapping, the GC pauses tend to increase to levels not tolerable by most applications.

java.lang.OutOfMemoryError: Out of swap space? is often caused by operating system level issues, such as:

  • The operating system is configured with insufficient swap space.
  • Another process on the system is consuming all memory resources.
Читайте также:  Php узнать версию пакета

It is also possible that the application fails due to a native leak, for example, if application or library code continuously allocates memory but does not release it to the operating system.

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:

swapoff -a dd if=/dev/zero of=swapfile bs=1024 count=655360 mkswap swapfile swapon swapfile

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.

© Copyright Plumbr. All Rights Reserved.

Источник

Constantly Increasing swap size in Linux and Swap space not being reclaimed?

I have a 8GB RAM linux box on which 4 tomcat servers are running. One of the them is set to 3000MB memory(jvm -Xms and -Xmx setting) and others are set to 1500MB. The swap partition is also set to 8Gigs. When I start these servers, the swap file usage is low. But over a period of days and during certain times when one/all of the servers are in peak activity , the swap usage starts increasing. Here’s a typical sar -r output.

kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree kbswpused %swpused kbswpcad 48260 8125832 99.41 196440 2761852 7197688 1190912 14.20 316044 75504 8098588 99.08 198032 2399460 7197688 1190912 14.20 316032

It shows 14.2% swap used currently. The funny thing is this % NEVER decreases. It continues to increase and reach up to 30-40%. We restart our servers weekly. I would assume the %swpused to increase during periods of peak activity and decrease during periods of low activity..Or at least remain constant. This looks like the swap space is never reclaimed by the OS.. Output of free : free -m total used free shared buffers cached Mem: 7982 7937 45 0 32 2088 -/+ buffers/cache: 5816 2166 Swap: 8191 1163 7028 So there’s at least 2g of free Ram. So the question is Why is the swap space continuing to increase and not being reclaimed by the OS ? Or how to debug this to figure out whats hapenning..

Источник

Оцените статью