Linux Performance: Why You Should Almost Always Add Swap Space
If you must use swap (you probably don&#x27;t need to), then at least set zswap.enabled=1 in the kernel boot options, typically in grub.  This will enable lzo compression of swap in memory so there is less writing to disk.  Some newer kernels use lz4.<p>Either way, what most folks are actually missing is the correct kernel settings for the amount of memory they have.  Sadly, the kernel does not dynamically adjust these based on your total amount of memory.  Below is based loosely on Redhat suggestions.  Please note, that if you fall below the min free, the kernel will decide what to do next, based on your oom and panic settings.  The settings below will have the kernel free cache and other memory earlier so that you do not hit those stalling conditions and can even prevent some OOM race conditions.  Some might suggest tuned, but use tuned with caution or at least read up on everything it does.<p><pre><code>    MEM=`grep ^MemTotal &#x2F;proc&#x2F;meminfo | awk {&#x27;print $2&#x27;}`
    if   [ ${MEM} -gt 1129241478 ] ; then
        sysctl -q -w vm.min_free_kbytes=16384000
    elif [ ${MEM} -gt 564620739 ] ; then
        sysctl -q -w vm.min_free_kbytes=8192000
    elif [ ${MEM} -gt 352887962 ] ; then
        sysctl -q -w vm.min_free_kbytes=4096000
    elif [ ${MEM} -gt 176443981 ] ; then
        sysctl -q -w vm.min_free_kbytes=1024000
    elif [ ${MEM} -gt 88221990 ] ; then
        sysctl -q -w vm.min_free_kbytes=524288
    else
        sysctl -q -w vm.min_free_kbytes=262144
    fi
</code></pre>
If you have small VM&#x27;s, then perhaps set the default above to something a little smaller.  You can of course free up about 128MB on default installations by removing &quot;crashkernel&quot; from your grub config and rebooting.<p>Then do this regardless, because overcommit set to 0 does not mean off, so we set the ration to 0 as well.  Overcommit is good for developers testing code and finding the correct ways to manage memory in their applications during development.<p><pre><code>    sysctl -q -w vm.overcommit_ratio=0
</code></pre>
And of course, cache pressure plays into early evacuation of the right cache based on your usage:<p><pre><code>    ## default is 100 (optimal for file servers).  4000+ for in memory databases.
    ## 10000 means always prefer page cache.
        sysctl -q -w vm.vfs_cache_pressure=1000
</code></pre>
And if you have people oversubscribing a lot: (adjust based on your memory capacity)<p><pre><code>    sysctl -q -w vm.admin_reserve_kbytes=131072
    sysctl -q -w vm.user_reserve_kbytes=262144
</code></pre>
Please do read up on all of these before testing on your test machines. [1]<p>[1] <a href="https:&#x2F;&#x2F;www.kernel.org&#x2F;doc&#x2F;Documentation&#x2F;sysctl&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.kernel.org&#x2F;doc&#x2F;Documentation&#x2F;sysctl&#x2F;</a><p>Then finally, make sure you have Transparent Huge Pages disabled unless you know for sure you need it.  THP can leak a lot of memory and it is nearly impossible to see without extensive kernel debugging.<p>In grub, set this and reboot:<p><pre><code>    transparent_hugepage=madvise
</code></pre>
Or to manually disable THP during run-time,<p><pre><code>    echo -n &quot;madvise&quot; &gt; &#x2F;sys&#x2F;kernel&#x2F;mm&#x2F;transparent_hugepage&#x2F;enabled
    echo -n &quot;never&quot; &gt; &#x2F;sys&#x2F;kernel&#x2F;mm&#x2F;transparent_hugepage&#x2F;defrag
</code></pre>
Then restart your applications.  THP defrag can also cause stalling and lag spikes, especially in large memory java deployments (it will look like FGC&#x27;s) and in MongoDB, Cassandra, others.<p>If you did this manually, stop your apps, flush cache, compact memory, then start your apps.<p><pre><code>    sync;sync;sync
    echo 3 &gt; &#x2F;proc&#x2F;sys&#x2F;vm&#x2F;drop_caches
    echo 1 &gt; &#x2F;proc&#x2F;sys&#x2F;vm&#x2F;compact_memory
</code></pre>
Some will say 3 sync&#x27;s is not required.  This is mostly true, but some old raid controllers treat this differently.