All About Linux Swap Part 2: Management

In: Linux


2 Feb 2009

Note: This article is the second 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 appears below and highlights basic analysis and management techniques for handling swap space. Part 3 will discuss the current state of swap usage and present opinions about its implementation today.

Exploring Swap

If you’re new to Linux and choose a guided installation where the partitioning was automatically configured for you, you may not even be aware of how much swap space you’ve allocated and where exactly it lives. Fortunately, there are a couple different ways that allows us to explore, analyze and manage this information.

swapon/swapoff

swapon -s

swapon is part of a pair of commands (its complement being swapoff) intended to wholly toggle the state of swap. Specifically, man states, “enable/disable devices and files for paging and swapping”. The -s parameter simply displays current usage. The output should look similar to this:

Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       2000084 5452    -1

This result tells us a few things:

  • Filename: /dev/sda3
    • This is the actual device that swap lives on
  • Type: partition
    • This filename is actually a partition, rather than a file. Swap can live in either type of device
  • Size: 2000084
    • The size in KB of the device/file. In this case, 2GB
  • Used: 5452
    • The amount of space currently in use in KB. Just over 5MB currently
  • Priority: -1
    • This is an arbitrary number (typically from 0 through 10) that allows the user to configure the order in which Linux should utilize swap space (highest to lowest). Most users will only have a single swap file, but it is certainly possible to have multiple and spread them among several different files/partitions.

More information on priority: A scenario that might exist in which you would need this, is if you decide to add another swap, but have it within a file rather than partition new space for it. You can set the partition to a priority of 5 and the new swap file to Priority of 1. Because the highest value is always preferred, this will ensure that the swap partition is used before the swap file. Another case is spreading the swap over multiple separate drives. If the priority is set the same for both swap devices, the kernel will utilize them in a manner similar to a striped array (round-robin). When it comes time to write to disk, this can help increase performance a bit.

free

Another utility, free, doesn’t give us anymore information than swapon -s, but it allows us to see swap usage in the context of overall system memory.

free -mot

The -m parameter simply displays the values in MB rather than KB. -t gives us a grand total at the bottom. The -o option hides some information we’ll get to shortly. The output might look something like this:

             total       used       free     shared    buffers     cached
Mem:          3959       3934         24          0        158       1957
Swap:         1953          5       1947
Total:        5912       3940       1972

There are 3 lines in the output: Memory, Swap, and the aggregate Total. This gives a better overall picture as to what memory is allocated where. Most of the lines are self-explanatory, but it’s important to note that the very low value of free memory is somewhat misleading. In this example, there is usable free memory of 2140MB.

Without getting too far into the details, think of this as saving a seat next to you in the movie theater. Maybe a friend was joining you for a movie, but in an emergency they had to leave during the previews. Most people assume this seat is reserved, until the theater begins to reach capacity, in which case the seat might really be needed. This extra seat is analogous to cached memory. It was in use at one time, and is still being saved in case your friend comes back, but could be used by another person. Modern OS’s handle memory the same way: They mark the space as used, but available.

To get a simpler picture of how much space we have, we need to add the buffers and cached columns back into the free column, because after all, the seat is technically empty. We can accomplish this automatically by removing the -o and -t parameter from our example (free -m):

             total       used       free     shared    buffers     cached
Mem:          3959       3934         24          0        158       1957
-/+ buffers/cache:       1818       2140
Swap:         1953          5       1947

Now, compare these values to the swap space. Although there is plenty of memory free, some swap is being utilized – a piddly 5MB of 1953MB – nothing to worry about. The final line, total sums things up, indicating we have 5912MB of usable space between all memory and swap, of which 3940 is in use, leaving 1972MB.

The -/+ buffers/cache sub-item simply summarizes how much used or free memory we actually have, and it becomes clear that there is plenty of available room for new applications. So why is swap being used up at all? Without exploring the gritty details, I can offer a much simpler solution: adjust how quickly swap is utilized.

Controlling swap

Because swap is intended as a supplement to memory, the process by which items are swapped in and out is automatic, giving the user little choice what goes into it. However, since kernel 2.6 a small tweak was added to configure how quickly the system swaps items – swappiness.

Swappiness is an arbitrary number from 0 to 100 that indicates how fast you want the system to page items out to disk, freeing up precious RAM. The higher the number, the sooner pages will be written out to swap. You can alter swappiness either temporarily or permanently, as outlined below. Before you go about that, check the value currently set:

sysctl vm.swappiness

Temporary

A simple terminal command is all that is required to change swappiness for the current session. If you want to keep things in memory longer than the current value provides, try lowering swappiness. For example, setting a value of 40:

sudo sysctl vm.swappiness=40

The nice thing about this, is that you can change the setting without rebooting and find a nice threshold that suits your needs. After restarting, the settings will be returned to the default value, so once you find a preferred value, you may decide to change swappiness permanently.

Permanent

This is only slightly harder, but not by much. It actually involved adding or editing a parameter in a single file.

sudo nano /etc/sysctl.conf

Then press Ctrl+W to search for “vm.swappiness”. If nano finds it, great! Just change the value from the current to your desired. If the value is not found, scroll to the bottom of sysctl.conf and add it. In either case, it should look like this:

vm.swappiness=40

On succeeding boots, this value will be the default. We can re-read the config file, verifying any changes:

sudo sysctl -p

Turn-offs and Turn-ons

There’s one last thing we can actually do to control swap: completely disable and enable it. This brings us back to the first section on swapon and swapoff. There are several reasons why one might want to completely turn swap off. One of those reasons is to flush whatever is in swap back to memory. As long as you actually have enough system memory for the contents of swap, you’re not going to break anything, but it might by worthwhile to ask yourself what the system might have swapped the pages out in the first place. Maybe there’s a runaway process leaking memory. Assuming everything is acting appropriately, in the long run, it is probably better to adjust swappiness as detailed above.

If you’ve decided that you want to move pages out of swap and back into memory, all you need is a couple commands, which we can combine into a single line:

sudo swapoff -a && sudo swapon -a

The -a option on both of these command stands for “auto”. To be a more specific, it tells them to operate on all swap devices located in /etc/fstab that aren’t explicitly marked at “noauto”. Don’t worry about the details for now, just know it works.

After running this command, my memory looked like this:

             total       used       free     shared    buffers     cached
Mem:          3959       3935         23          0        159       1956
-/+ buffers/cache:       1819       2139
Swap:         1953          0       1953

Notice that Swap now reports 0MB used, with a small decrease in the amount of cached and free memory. If there’s a lot of data in swap, it may take a bit of time for this to finish. Don’t panic, but instead, open another terminal and observe your memory usage:

free -ms 1

Again, -m reports usage in MB. -s 1 activates a continuous polling delay of 1 second. In other words, free -m will continue to run every 1 second until you tell it to stop with Ctrl+C. If you use this while swapoff is at work, you should see all the memory numbers adjust magically.

Last Resort

If course, if all else fails, a simple reboot will clear out not only your swap, but also anything in memory.

Wrap-up

Fortunately, Linux allows us to tweak many settings and the GNU/Linux toolset gives us a great way to handle these changes. Hopefully, this article has shed some light on how to manage that mysterious swap partition and will provide a jump point to learning more about the specific commands involved.

Part 3 will be less technical, instead focusing on a discussion about modern swap usage and how that impacts the end-user today.

  • Share/Bookmark

1 Response to All About Linux Swap Part 2: Management

Avatar

All About Linux Swap Part 3: Analysis | Idea Excursion

February 6th, 2009 at 12:53 pm

[...] 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 [...]

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.