Linux Performance: Why You Should Almost Always Add Swap Space

We know that using Linux swap space instead of RAM (memory) can greatly slow down the performance. So, one might ask, since I have more than enough memory available, wouldn’t it be better to remove the swap space? The short answer is no. There are performance benefits when swap space is enabled, even when you have more than enough RAM. See also Update, Part 2: Linux Performance: Almost Always Add Swap (ZRAM).

Even with more than enough server memory installed, you’ll often find that swap space will be used up after a long period of uptime. Check out the example below from a live-chat server with an uptime of about one month:

              total        used        free      shared  buff/cache   available
Mem:           3.7G        1.0G        445M         84M        2.2G        2.2G
Swap:          1.8G        308M        1.5G

Production of free -h Here 308M shows the swap space used. When I ran the check for swapping, there were no signs of ongoing or untimely swap I/O activity. Also, the kswap service did not consume much CPU time. In fact, the kswap process was nowhere near the top (top processes ranked by CPU time). To confirm, I used the following command:

ps -A | grep kswap
 40 ? 00:00:29 kswapd0

…so in this case, as in many, the use of swap is not hurting the performance of the Linux server. Now, let’s see how swap space can actually be help Linux server performance.

Update: Recently, I installed Manjoro i3 on my Pinebook Pro. It now comes with ZRAM enabled by default.

Benefits of Swap Space on Systems with Sufficient RAM

This is normal and for Linux systems it can be a good thing to use some swap even though RAM is still available. Linux kernel will speed up memory pages that are rarely used Swap space to ensure that even more cacheable space is made available in memory for frequently used memory pages (page is a piece of memory). Swap usage becomes a performance issue when the kernel is constantly pressured to swap memory pages in and out of memory.

Another advantage is that swap gives administrators time to react to low memory issues. We will often notice that the server is running slowly and upon login, will notice heavy swapping. Without swapping (as described in the next section), memory loss can lead to very sudden and severe chain reactions. So in general, I’d recommend setting the swap space to about the size of your largest process. For example, MySQL’s configured memory in my.cnf. It can also be small, especially if you have monitoring and/or alerts in place.

Some recommend that no swap or swap size be slightly larger than the total RAM. If you can give a valid reason for this, then that’s probably your choice. However, this is rarely the case on servers, and instead you should balance your decision with the effects swap will have on your specific applications. Swap does not change the amount of RAM required for a healthy server, Or desktop for that matter. It is designed to complement the performance of healthy systems.

In short:
— Even if there is still RAM available, the Linux kernel will move memory pages that are rarely used into swap space.
– It is better to swap memory pages that are idle for some time, while keeping frequently used data in the cache, and this should happen when the server is most idle, which is the purpose of the kernel.
– Avoid setting your swap space too large if it will result in performance issues, interruptions, or increase your response time (without proper monitoring/alerts).

Swap space vs no swap when available memory is low

Unlike the case above, if you don’t have enough memory, swap will be used frequently and will be more noticeable during any memory requirement spikes. If you do not have enough memory and no swap space, this will often lead to a failure to allocate memory for requests that require more memory pages. As a last resort, the kernel will deploy OOM killer To nuke high memory processes (usually MySQL, Java, etc.).

For a more detailed look at Linux swap space, read swap management And page frame reclamation chapter from Kernel.org In addition, refer to the last section, “kernel cache pressure and swapping,See my other blog post for tips on tuning Linux swap space usage by the kernel. If your swap space “used” is always ‘0’, you actually have a ton of freely available RAM, in which case It may be safe to delete swap space in … or you can adjust your kernel’s cache pressure to use even more RAM.

In short:
– Swap I/O scale is very poor. If memory pages cannot be swapped only when the server is idle, you should tune or disable swap. This is not usually the case, thus the “almost always” title of this blog post.
– With swap disabled, performance issues become noticeable very fast, and the OOM killer may be getting to you! ,

For comparison, here’s the output of free using an older version of free Props-of-3.3.1 On the same server:

             total       used       free     shared    buffers     cached
Mem:          3.7G       3.3G       445M         0B       4.2M       1.7G
-/+ buffers/cache:       1.6G       2.1G
Swap:         1.8G       308M       1.5G

Kernel cache pressure and swapping

Now that you have enabled swap. Consider adjusting your server’s cache pressure and propensity to swap (vm.swappiness) by following the guide below, which is from a previous article: Linux Server Needing a RAM Upgrade? Check with top, free, vmstat, and sar:

vfs_cache_pressure Controls the tendency of the kernel to reclaim memory, which is used for caching directory and inode objects. (default=100, recommend value 50 to 200)

exchange – This control is used to define how aggressively the kernel will swap memory pages. Higher values ​​will increase aggression; Lower values ​​reduce the amount of swap. (default = 60, recommended value between 1 and 60) Remove your swap for a 0 value, but this is generally not recommended in most cases.

To edit, you can add or change these lines in /etc/sysctl.conf file. For example, if you have low memory until you upgrade, you can try something like this:

vm.swappiness=10
vm.vfs_cache_pressure=200

This will increase cache pressure, which may seem counterintuitive to some extent because caching is good for performance. However, doing too many frequent swaps can significantly degrade the overall performance of your server. So not keeping much cache in memory will help to reduce swap activity. Also, setting vm.swappiness to 10 or less than 1 will reduce disk swapping.

On a healthy server with lots of available memory, use the following:

vm.swappiness=10
vm.vfs_cache_pressure=50

This will reduce the cash pressure. Since caching is good for performance, we want to keep the cached data in memory for a longer period of time. As the cache will get bigger, we still want to minimize swapping so that swap I/O doesn’t increase.

Use these commands to check the current values ​​using:

sudo cat /proc/sys/vm/swappiness
sudo cat /proc/sys/vm/vfs_cache_pressure

To temporarily enable these settings without rebooting, use the following commands:

sudo sysctl -w vm.swappiness=10
sudo sysctl -w vm.vfs_cache_pressure=50

The man pages of both versions of free should be considered independently. Check out the screenshot of the latest version below.

Leave a Comment