We know that using Linux swap space instead of RAM (memory) can seriously degrade performance. So, one might ask, since I have enough available memory, wouldn’t it be better to remove the swap space? The short answer is no. Enabling swap space brings performance benefits, even if you have more than enough RAM.
Even with enough server memory installed, you will often find that you will use swap space after a long period of normal operation. See the following example from a live chat server with approximately one month of uptime.
free -h The output here shows that 308M of swap space is being used. When I run the swapping check, there is no indication of ongoing or untimely swap I/O activity. In addition, the kswap service does not consume much CPU time. In fact, the kswap process could not be found in top (the top processes sorted by CPU time). To confirm, I used the ps command.
… So in this case, as in many cases, swap usage does not degrade the performance of a Linux server. Now, let’s see how swap space actually improves Linux server performance.
Advantages of swapping space on a system with sufficient RAM
It is normal and a good thing for Linux systems to use some swap even if there is still available RAM. the Linux kernel moves almost never used memory pages into swap space to ensure that more cacheable space is available in memory for more frequently used memory pages (a page is a piece of memory). Swap usage can become a performance problem when the kernel is forced to constantly move memory pages in and out of memory and swap space swap.
Another advantage is that swap gives the administrator time to react to low memory problems. We often notice servers running slowly and notice a lot of swaps when logging in. Without swap (as described in the next section), the lack of memory can have a more abrupt and severe knock-on effect. So I would recommend setting the swap space to the size of your largest process. For example, the memory configured by mysql in my.cnf.
Some people recommend not swapping or swapping to a size slightly larger than the total RAM. if you can justify this, then this may be the option for you. However, this is hardly the case on a server and you should balance your decision with the impact of Swap on your particular application. Swap does not change the amount of RAM required by the server . It is designed to improve the performance of your system.
- Even if there is still available RAM, the Linux kernel will move memory pages that are almost never used into the swap space.
- It is better to swap out memory pages that have been inactive for a while and keep frequently used data in the cache, which should happen when the server is most idle, which is the goal of the kernel.
- Avoid setting the swap space too large, as this can lead to performance problems, outages, or longer response times.
Swap space vs. not using swap space when there is not enough memory available
Unlike the case above, if you do not have enough memory, the swap will be used frequently and significantly more during any spikes in memory demand. If you do not have enough memory and do not have swap space, this usually results in an inability to allocate memory for requests that require more memory pages. As a last resort, the kernel will deploy OOM killers to attack high memory processes (usually MySQL, java, etc.).
If your swap space “used” is always “0”, then you do have a lot of free RAM available, in which case it is probably best to delete the swap space.
- Swap I/O scales poorly. If it is impossible to swap memory pages only when the server is idle, Swap should be tuned or disabled.
- With Swap disabled, performance problems will become apparent very quickly and the OOM killer may catch you! :)
For comparison, here is the output of an older version of free from procps-ng-3.3.1 on the same server.
Kernel cache pressure and swapping
You now have swap enabled. Consider adjusting the server’s cache pressure and swap vm.swappiness according to the following guidelines.
vfs_cache_pressure - Controls how often the kernel reclaims memory for caching directories and inode objects. (Default value = 100, recommended values 50 to 200)
swappiness - This value is used to define how aggressively the kernel swaps memory pages. Higher values increase aggressiveness; lower values decrease swappiness. (Default value = 60, recommended values are between 1 and 60) Remove the 0 value for swapping, but it is usually not recommended in most cases.
To edit, you can add or replace these lines in the /etc/sysctl.conf file . For example, if you are low on memory before upgrading, you can try the following.
This will increase cache pressure, which seems somewhat counterproductive, as caching is good for performance. However, too frequent swapping can significantly reduce the overall performance of the server. Therefore, not keeping as much cache in memory as possible will help reduce swapping activity. In addition, setting vm.swappiness to 10 or as low as 1 will reduce disk swapping.
On a server with a large amount of available memory, use the following command.
This will reduce cache pressure. Since caching is good for performance, we want to keep cached data in memory for longer. Since the cache will get larger, we still want to reduce the swap to not increase the swap I/O.
To check the current value using these commands, use:
To temporarily enable these settings without rebooting, use the following command.