All About Linux Swap Part 3: Analysis

In: Linux


6 Feb 2009

After familiarizing yourself with swap, you might have a few questions. How much swap space? What do I set swappiness to? Do I even need swap anymore?

Note: This article is the third in a multipart series introducing the Linux swap. Part 1 is intended to familiarize the reader with the concept of swapping, why it exists, and what it’s used for. Part 2 highlighted basic analysis and management techniques for handling swap space. Part 3 appears below and discusses the current state of swap usage and present opinions about its implementation today.

Questions

Several questions are repeatedly posed in regards to swap:

  • How much swap space do I need?
  • What is the right settings for swappiness?
  • With so much memory, do I even need swap?

I want to address each of these questions and hopefully provide a larger understanding of swap in the context of everyday use.

How much swap space do I need?

This is probably the most-asked question in regards to swap, and the answer isn’t entirely clear-cut, however, there are a couple of rules to be guided by.

In a strictly technical sense, the amount of swap you’ll need should be the total amount of virtual memory you’ll need minus system memory. In other words,

Total Virtual Memory - Memory = Swap

Total Virtual Memory - Memory = Swap

Traditionally, the recommendation was twice the amount of RAM. However, this was many years ago when 128MB (or less) might have been standard, resulting in a 256MB swap partition, totaling as much as 384MB of virtual memory. This pales in comparison to today, when 2GB is a standard amount on desktops. It is unlikely that a server with 16GB of RAM will ever need an additional 32GB of swap.

Desktop

Remember that virtual memory is a mapping of all available memory to a system, no matter the device type. Therefore, if you anticipate that you will never utilize more than 4GB of virtual memory and have 2GB of physical memory, 2GB might be a good amount for you. But better than 2GB of swap would be an additional 2GB of memory, allowing your heaviest usage to wholly fit inside RAM.

On my desktop at home (which currently runs Gnome + Compiz on Ubuntu 8.10 Intrepid Ibex), I started with 2GB of RAM and a 2GB swap partition. I’ve since upgraded the memory to 4GB and – as expected – seen swap usage drop to virtually none. The only situation in which my swap fills up, is when a process runs amok and grabs every last piece of memory available. Although I practically never reach into that extra memory, I was thankful it was there instead of having OOM Killer wreak havoc or forcing my system to a screeching halt.

Because the old “two times” model is bunk, some are recommending a new allocation guideline that goes something like this:

RAM Swap
≤ 2GB = RAM
> 2GB = 2GB
Laptop

Assuming similar usage patterns, you can follow the guidelines as a desktop, with one major exception. Because of the ability to hibernate – if you plan to use this functionality – allocate at least as much swap as you have of system memory. This is especially important during partitioning, because Linux cannot hibernate to a swap file.

Server

Generally speaking, servers tend to be loaded with memory for speed and efficiency, reducing the tendency to page to disk. Because they are often designed with a particular function and load in mind, swap may be of little need. In fact, if the server is adversely swapping, it is likely indicative of a more serious problem requiring more memory or application changes.

A consequence of this would be a small amount of swap (half, quarter, or less of system memory). When provisioning the server, you might instead decide that the workload and applications have been set in a very specific way (i.e. a VM server) and would prefer no swap at all. I wouldn’t recommend this, but it’s certainly possible from a technical and practical standpoint.

There are, of course, exceptions to these rules. If you anticipate gigantic workloads where you cannot reasonably fit the entire contents into memory (i.e. very large datasets on heavy-traffic, multi-user database systems), you may have no other choice than to allocate lots of swap space.

Red Hat’s own recommendations for RHEL5 are as follows:

RAM Swap
≤ 4GB ≥ 2GB
4GB – 16GB ≥ 4GB
16GB – 64GB ≥ 8GB
64GB – 256GB ≥ 16GB

What is the right settings for swappiness?

This is an entirely subjective setting, but there are a few strong opinions on what they feel is best. Because there is no way to anticipate the workload of an OS on installation, default swapiness setting falls somewhere in the middle. For example, on Ubuntu, the default value is 60.

Very high

One 2.6 kernel maintainer insists that a very aggressive setting is preferred (i.e. 100). His logic is that wasting a lot of memory on large, bloated applications is not an ideal use of that memory space; instead, force those pages out to disk, freeing up memory for useful items. The counter to this argument is that this produces the undesired behavior of delayed interactivity between the user and system when switching back to that application, because the operating system is busy reading the pages off disk back into memory. A further concern of this behavior is that cache is being given greater importance than applications – a useful scenario in servers and development workstations, but less so for general desktop use.

Very Low

For the average end-user, system responsiveness likely trumps cache performance, so there is a tendency for users that tweak swappiness towards the bottom. Though many have tried setting swappiness to 0, they found it unworkable; instead, many report a better experience with a setting of 10. A group of members have even proposed that this value be tuned lower by default in Ubuntu. Telling the kernel to swap as little as possible also has the effect of “as late as possible”. If memory cannot entirely cover the requirements of the next process, the kernel has no choice but to swap immediately, making loading the process slower. This speaks to the argument for a high setting if your system is typically memory-constrained.

With so much memory, do I even need swap?

Even if you decide that you have copious amounts of memory for the intended tasks, the inexpensiveness of disk space makes not having at least a small swap space (256MB) ludicrous. Additionally, the kernel can do strange things when there is no swap available, throwing out-of-memory errors and killing processes, making this decision a no-brainier. Disk is cheap, so allocate some space for swap, even if as a swap file rather than partition.

Final Thoughts

As with most things in life, the answer to how to configure swap is, “It depends”. Your specific situation largely dictates how much space and how quickly the kernel should page items out to disk. Much like a financial budget, you make a best estimation at first and then changes as the situation requires. Aside from a few specific instances, you can grow or shrink swap area as your needs change.

With the additional knowledge on what swap is used for and how to gauge its importance to your workload, you can start making a better-informed decision on how much you need and how to configure it. Take a look back at Part 2 and use those tools to measure how your system is responding to the hardware constraints and system settings. Even with all this in mind, there is a single overriding rule to swap space: more memory is better.

  • Share/Bookmark

3 Responses to All About Linux Swap Part 3: Analysis

Avatar

stan_qaz

February 6th, 2009 at 6:08 pm

Don’t overlook the need for swap if using some of the hibernate options. This was briefly mentioned in part 1.

Avatar

Trenton D. Adams

March 10th, 2009 at 12:45 pm

I have used no swap for a couple of years. I was sick of my system swapping so much that I just quit using a swap file all together. At the time, I didn’t know that the kernel was configurable in this regard, so I just worked without swap. I have 4G of memory, and I NEVER get out of memory errors. I’m a Java developer, so that’s pretty amazing!!! HEHE.

Recently I went searching for the configurability of swappiness. It is documented in the kernel source. So, I’m trying out using swap again, but with a swappiness of 0. I haven’t seen any problems yet, and I honestly don’t expect any. So yeah, I think Andrew Morton’s view is a legacy one. I understand his point, and I would have agreed with 256M of RAM, but things really have changed. 4G is common place these days.

I think with swap disabled, or swappiness set to 0, the kernel does an extremely good job of managing system cache. The system cache is always high UNLESS the kernel needs to allocate more memory. I’m usually seeing >= 1G system cache, with a few M free, and I appreciate that.

As for the kernel killing processes, when out of memory, I don’t have a problem with that at all. I just think the default behaviour for that is, well, REALLY BAD. The default is to kill the one with the most memory. Well, on a server, that’s a REALLY BAD THING. Suddenly a service could be down, and you didn’t know. So, I like to change the default behaviour like so…

echo 1 > /proc/sys/vm/oom_kill_allocating_task

This will cause the allocating task that ran out of memory to be killed. Usually, this is what I want, *I think*. However, I am uncertain of the behaviour if you are really at your memory peak. But, I’m trying it out, and I’ll see what happens.

Avatar

Taylor Gerring

March 10th, 2009 at 3:43 pm

I’ve been very happy turning swappiness down from 60 to 40. I don’t often fill all 4GB of RAM, so this is enough of a change to prevent swap being used unnecessarily without turning it off completely.

Comment Form

  • ChrisC: Does anyone have information on linking SQL2005 with a SQLite sever? [...]
  • Mark: I was able to add my Google calendar using the above instructions with no problems, but I still cann [...]
  • Kat: This worked wonderfully and easily imported my husband's calendar as well. Thanks! [...]
  • DaveC: I'm trying to link a MYSQL database (held on a LINUX server) as a linked server through SQL Server 2 [...]
  • Taylor Gerring: Currently, the only way to synchronize contacts is by using Exchange or MobileMe. [...]


This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States.