title: Swappiness in JVM
body: <p>I stumbled upon some interesting problems last week where I noticed that the one of our production servers, which has <a href="https://en.wikipedia.org/wiki/Apache_HTTP_Server" rel="nofollow noreferrer">Apache HTTP Server</a> over <a href="https://en.wikipedia.org/wiki/Apache_Tomcat" rel="nofollow noreferrer">Tomcat</a> running, stopped, reporting an HTTP outage.</p>
  <p>On further investigating this issue it seemed like it was due to JVM causing memory pages to be swapped out quickly. This resulted in the swap space being completely populated, causing a memory issue the next time a page is moved to swap.</p>
  <p>Investigating further, it seems that there is a JVM swappiness factor that's set to 60% by default with some of the our Linux distributions. Based on some research, it seems that this could be a high value for a web service that has high traffic. Our swap space was set to 2 GB.</p>
  <p>The swap details are:</p>
  <pre><code> Filename                                Type            Size    Used    Priority
   /dev/sda3                               partition       2096472 1261420 -1
  
   From /proc/meminfo
   SwapCached:     944668 kB
  </code></pre>
  <p>Our JVM properties are as follows:</p>
  <p>-Xmx6g -Xms4g -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:PermSize=512M -XX:MaxPermSize=1024M -XX:NewSize=2g -XX:MaxNewSize=2g -XX:ParallelGCThreads=8</p>
  <p>The server runs with 12 GB RAM.</p>
  <p>Swappiness does not go well with the JVM's <a href="https://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29" rel="nofollow noreferrer">GC</a> process. Thus I tried reducing the swappiness to 0, but that did not change anything. We still see cases where the entire swap space is consumed and resulting in an OutOfMemory error.</p>
  <p>How can I tune the JVM performance?</p>