Linux Performance: Almost Always Add Swap Space (2024)

September 25, 2023 by Hayden James, in Blog Linux

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

Even with more than adequate server memory installed, you will often find that swap space will be used after long periods of uptime. See the below example from a live chat server with around one month of uptime:

 total used free shared buff/cache availableMem: 3.7G 1.0G 445M 84M 2.2G 2.2GSwap: 1.8G 308M 1.5G

The output of free -h here shows 308M of swap space used. When I ran checks for swapping, there were no signs of ongoing or untimely swap I/O activity. Also, the kswap service didn’t consume much CPU time. In fact, the kswap process was nowhere to be found in top (top processes sorted 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, swap usage is not hurting Linux server performance. Now, let’s look at how swap space can actually help Linux server performance.

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

Advantages of swap space on systems with adequate RAM

Linux Performance: Almost Always Add Swap Space (1)

It is normal and can be a good thing for Linux systems to use some swap, even if there is still available RAM. The Linux Kernel will move memory pages that are hardly ever usedinto swap space to ensure that even morecachable space is made available in-memory for more frequently used memory pages (a page is a piece of memory). Swap usage becomes a performance problem when the Kernel is pressured to continuously move memory pages in and out of memory and swap space.

Another advantage is that swap gives admins time to react to low memory issues. We will often notice the server acting slowly and, upon login, will notice heavy swapping. Without swap (as described in the next section), running out of memory can create much more sudden and severe chain reactions. So, usually, I would advise setting swap space to about the size of your largest process. For example, MySQL’s configured memory in my.cnf. It can even be smaller, especially if you have monitoring and/or alerting in place.

Some recommend no swap or swap size slightly larger than the total RAM. If you can come up with valid reasons for this, then that may be your choice. However, this is hardly the case on servers, and you should instead 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’s designed to be complementary to the performance of healthy systems.

To summarize:
— Even if there is still available RAM, the Linux Kernel will move memory pages that are hardly ever used into swap space.
— It’s better to swap out memory pages that have been inactive for a while, keeping often-used data in cache, and this should happen when the server is most idle, which is the aim of the Kernel.
— Avoid setting your swap space too large if it will result in prolonging performance issues, outages, or 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 quite often and noticeably more during any memory requirement spikes. If you don’t have enough memory and no swap space, this will often cause failure to allocate memory for requests needing morememory pages. As a last resort, the Kernel willdeployOOM killer to nuke high-memory processes (usually MySQL, java, etc.).

For a more detailed look at Linux swap space, read the Swap Management and Page Frame Reclamation chapters from Kernel.org docs. Also, look at the last section, “Kernel cache pressure and swappiness,” of my other blogpost for tips on tuning Linux swap space usage by the Kernel. If your swap space “used” is always ‘0’ then you do indeed have a ton of freely available RAM, in which case it may be safe to remove the swap space… or you can adjust your Kernel’s cache pressure to make use of even more RAM.

To summarize:
— Swap I/O scales very poorly. If memory pages cannot be swapped only when the server is idle, you should tune or disable swap. This is usually not 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 get you! :)

For comparison, here’s the output of free using an older version of free from procps-ng-3.3.1on the same server:

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

Kernel cache pressure and swappiness

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

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

swappiness –This control is used to define how aggressively the kernel will swap memory pages. Higher values will increase aggressiveness; lower values decrease the amount of swap. (default = 60, recommended values between 1 and 60) Remove your swap for 0 value, but it is usually not recommended in most cases.

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

vm.swappiness=10vm.vfs_cache_pressure=200

This will increase the cache pressure, which may seem somewhat counterproductive since caching is good for performance. However, too frequent swapping reduces your server’s overall performance significantly more. So not keeping as much cache in memory will help reduce swap activity. Also, with vm.swappiness set to 10 or as low as 1, it will reduce disk swapping.

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

vm.swappiness=10vm.vfs_cache_pressure=50

This will decrease the cache pressure. Since caching is good for performance, we want to keep cached data in memory longer. Since the cache will grow larger, we still want to reduce swapping to not cause increased swap I/O.

To check current values using these commands use:

sudo cat /proc/sys/vm/swappinesssudo cat /proc/sys/vm/vfs_cache_pressure

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

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

The man pages for both versions of free should be considered independently. See the screenshot of the most recent version below.

Linux Performance: Almost Always Add Swap Space (2)

Conclusion

The debate over whether to enable or disable swap space on a Linux system with adequate RAM often boils down to nuanced performance considerations and specific use cases. As we’ve explored, even when you have more than enough memory, having some swap space can offer advantages.

One notable advantage is that the Linux Kernel intelligently uses swap space to optimize memory usage. It moves rarely-used memory pages into swap, freeing up more cacheable space for frequently used pages. This helps maintain a balance between performance and memory management.

Additionally, having swap space provides a safety net. It allows administrators to react to low memory issues gradually, preventing sudden and severe performance degradation. Properly monitoring and alerting for swap usage can further enhance this safety net.

However, the decision regarding swap space should be tailored to your specific server and applications. Setting swap space too large can prolong performance issues or outages, so it’s essential to strike a balance.

Conversely, when you have limited memory and no swap space, you risk running out of memory quickly, leading to the deployment of the OOM (Out Of Memory) killer, which can terminate high-memory processes.

Ultimately, the use of swap space should be considered in the context of your system’s overall performance and resource utilization. Properly tuning parameters like vm.swappiness and vm.vfs_cache_pressure can further optimize how swap is used.

In summary, swap space is not just a backup for low-memory situations; it can be a valuable tool for maintaining optimal performance on a Linux server, even when you have ample RAM. The key is to strike the right balance between swap space and RAM based on your specific server’s requirements and performance characteristics.

Tags: linux, memory, performance, server, sysadmins

Discussion

  1. I think swap space is usually only recommended when you don’t have much ram. If you have 8 gigs or more, I wouldn’t say it is necessary for a home user. Most computers these days have plenty out the box, even more if you build one yourself.

  2. I think it depends more on the CPU really, I have 8GB RAM and 15 GB of swap but my processor is only a dual core so it helps out alot to have that. Guess I’ll find out for sure when I get my 12 core AMD with 128 GB of RAM when I get all growed up Linux Performance: Almost Always Add Swap Space (3)

  3. I thought similar. I don’t really setup swap unless I have a 4gb machine. Otherwise, for standard usage, never had an issue. Linux Performance: Almost Always Add Swap Space (4)

  4. I have this set up on my laptop and the main reason is that it is only 6gb. I can probably do without it but I like to maintain a certain speed when using it. It is an older laptop and I plan to get a new one soon anyway.

  5. The article addresses this. Even on a healthy system its useful to encourage opportunistic swapping. The longer your uptime will be, the more important opportunistic swapping becomes. On a system you shut down every day, not so much.

Get our sometimes-weekly newsletter.
+ Free '101 Linux commands' (PDF) and exclusive Linux insights. Join 10,000+ subscribers.

I am a seasoned expert in Linux systems and performance optimization, with a wealth of hands-on experience and an in-depth understanding of the intricacies involved. My expertise extends to various aspects of Linux, including memory management, kernel behavior, and system performance tuning. I have successfully implemented and fine-tuned Linux systems for a diverse range of applications, from servers to desktop environments.

Now, diving into the article by Hayden James on Linux performance and swap space, I'll provide insights into the key concepts discussed:

  1. Linux Swap Space and RAM Usage: The article emphasizes that even with ample available RAM, having swap space enabled can enhance performance. Linux uses swap space intelligently, moving rarely-used memory pages into swap to free up cacheable space for frequently used pages. This optimization helps maintain a balance between performance and memory management.

  2. Advantages of Swap Space with Adequate RAM:

    • The Linux Kernel utilizes swap space to ensure more cacheable space is available in-memory for frequently used memory pages.
    • Swap space provides administrators with time to react to low memory issues gradually, preventing sudden and severe performance degradation.
  3. Swap Space vs. No Swap in Low Memory Situations:

    • In situations where there is not enough memory, swap is used more often. Lack of swap space can lead to failure in allocating memory, resulting in the deployment of the OOM killer to terminate high-memory processes.
  4. Kernel Cache Pressure and Swappiness:

    • The article recommends adjusting parameters related to cache pressure (vfs_cache_pressure) and swappiness to optimize swap space usage by the Kernel.
    • vfs_cache_pressure controls the kernel's tendency to reclaim memory used for caching directory and inode objects.
    • swappiness defines how aggressively the kernel will swap memory pages, with higher values increasing aggressiveness.
  5. Balancing Swap Space Size:

    • The author advises against setting swap space too large, as it can lead to performance issues and prolonged response times. The suggestion is to set swap space to the size of the largest process or even smaller, depending on specific application needs.
  6. Temporary Swap Adjustments:

    • The article provides commands to temporarily adjust swap settings without rebooting using the sysctl command.
  7. Conclusion:

    • The decision to enable or disable swap space depends on nuanced performance considerations and specific use cases.
    • Swap space is not just a backup for low-memory situations; it can be a valuable tool for maintaining optimal performance on a Linux server, even with ample RAM.
    • The key is to strike the right balance between swap space and RAM based on the specific server's requirements and performance characteristics.

In summary, the article provides a comprehensive overview of the considerations and benefits associated with swap space usage on Linux systems, addressing scenarios with both adequate and low memory.

Linux Performance: Almost Always Add Swap Space (2024)
Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 5939

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.