Swap Basics
The traditional (read: older) rule of thumb was to have a swap space at least the size of physical RAM, if not twice as large. However, you also need to consider growth. If you expect to increase your RAM in the future, you should consider that when you set how much space you are going to use for swap. RAM just slides into your system; increasing swap may require reinstalling the operating system, particularly if you have an older version of Linux. (perhaps a very old version)
So, how much do you assign to swap? Good question. In general, I still support the suggestion is twice as much as RAM. This is the “good reason” I mentioned for having more swap than physical RAM. Creating a large swap space is easier to do it now and waste the space than to reinstall later. Another good reason is when you have more than one user running graphical applications. In this case, then even setting swap to two times RAM is reasonable. If all the space is taken up on the primary hard disk, you can add a hard disk and use the swap command to add additional swap space.
The key factor is how many applications you will run and what kind of applications. You need to consider the behavior of the application. If you are doing work with some kind of graphics (i.e. graphic design, ray tracing, and so forth), then you might need to conside more swap. If you have a lot of users on your system, you might want more swap, as well.
Keep also in mind that accessing swap is slower than accessing RAM. You might want to consider adding more RAM. I have a co-worker who has 3 GB of RAM in his system as he does a lot of graphical work and it is an extreme burden on his system to be constantly swapping in and out. He never gets close to using all of his RAM, so he does not need a large swap space.
Versions of the Linux kernel prior to 2.4.10 “liked” to have at least twice as much swap as RAM. However, with the newer kernels, this is no longer true.
One important thing to consider is what happens when the system runs out of memory. Being out of memory (OOM) is a dangerous thing and can cause the whole system to come to a stand-still or even crash. Within the Linux memory management functions is something called an “OOM killer” which will stop (or kill) processes when the system runs out of memory. The problem is that currently (August 2006), processes are killed “blindly”. That is, without regard to their importance. In general, the system calculates how “bad” the process is longer a process runs and the more memory it uses, the greater the chance is that it will be killed. (For details look at the file /usr/src/linux/mm/oom_kill.c).
The biggest problem is that it arbitrary. That is a program could be running correctly, not causing any problems, but it just runs a lot and uses a lot of RAM. Imagine if this was an database. The argument whether the database should be killed or the whole system stops is philosophical, so we won’t go into it here. With 40 GB being a small hard disk today, allocating 1 GB swap (i.e. 2.5%) for peice of mind is not a bad idea.
You also need to keep in mind that swapping takes up system resources. The time to access the hard disk is hundreds of times slower than the time to access RAM. Therefore, if speed is an important consideration, you should think about having enough RAM so you don’t swap. The maximum size of your swap space depends on your hardware architecture and more recent kernels on the i386 can have swap partitions that are as large as 2Gb and you can have as many as 8 different swap partitions if you have kernel older than 2.4.10. Later versions support up to 32 swap spaces.
Note that I said swap spaces and not just swap device or swap partition. Linux allows you to create a swap file. Like any other file, a swap file exists on your filesystem and takes up space. The advantage is that you can add a swap file at any time, provided you have the space on the hard disk. You don’t need to to repartition your hard disk or even reboot.
There are two different swap versions (or formats). Kernels prior 2.4 supports only version 0 swap spaces. Versions later than Linux 2.1.117 support version 0 and version swap. However, Linux 2.6 only supports version 1. Therefore you need to be careful when upgrading. The mkswap command can format in either format. See the mkswap for more details.
Another change with the 2.4.10 kernel is that the swap spaces can be up to 64 Gb in size. Note, however, that with some Linux distributions, the mkswap command can only create swap devices that are 2GB or smaller.
Managing Swap
In many cases, once the system is installed, you never have to think about swap again. However, when you start using your system more actively, add new software, and so on, you will probably find that you should at least take a look at your current swap usage.
Linux provides a number of tools to monitor swap. The easiest is the free command, which gives you a quick overview of the memory usage. You can also use the top which can provide an self-updating view of your system, including memory usage by process, users on the system, and so forth. Also the /proc/swaps and /proc/meminfo files contain information about memory usage on your system.
Linux also provides tools to manage your swap space. You can add and remove spaces as you need to, as well as turn them on and off, even while they are being used.
To create a file to be used as swap, you need to first create the file. This is most easily done with the dd command. To create a 65 Mb file, you might have this command (from the the mkswap man-page):
which displays:
Next you have to prepare the file for usage as swap space using the mkswap. The simplest form would be:
Where “device” is either the name of a device node for a hard disk partition or the name of a file you want to use. The “size” option is only required when you create a swap file. However, it is actually superfluous and still maintained for backwards compatibility. The command you issue migt look like this:
Which displays:
What that this does is format the swap space by adding the swap header information. Note this said that it created a version 1 swap space. We could have used the -v0 option to create a version 0 swap space if we needed to.
One key thing to keep in mind is that the kernel needs to have the swap file created to it’s full size before it is used. That is, it cannot be a sparse file (one that only uses part of the allocated space).
At this point we are ready to activate the swap space. If you are adding the swap space permanently, then you will need to include it in your /etc/fstab file. My default (initial) swap space looks like this:
This basically says that the device /dev/hda5 is to be mounted onto the special mount point swap, is of type swap, has a priority of 42 and that the filesystem should not be dumped if the system crashes, nor should the filesystem be checked on system boot. (The last two 0’s)
To automatically use the swap file we just created, we might add an entry that looks like this:
When your system boots, all swap devices will be added which are listed in the /etc/fstab, unless they have the “noauto” option (just like any normal filesystem). If I wanted to immediate add the swap space without having to reboot, I can run swapon -a, which will activate all swap spaces in the /etc/fstab file (again, unless they have the “noauto” option). If the swap space is already in use, the system will silently ignore it.
As you might guess, the priority of the swap space determines the order in which the swap space is used. The higher the priority the sooner it will be used. In this example, the primary swap space in its own parition has a priority of 42 and will be used before the swap file with a priority of 5.
We can also add swap space dynamically using the swapon command. After creating the swap space, you might activate it with this command:
To show what is currently beeing used as swap space we issue the command
This might show us:
Just as you can enable swap from the command line, you can also turn it off. This is done with the swapoff command and it might look like this:
For more details see the swapoff man-page.
If performance is really an issue thare are number of different things you can do. If you have multiple hard disk on different controllers, you can put swap spaces on each device and give them the same priority. This works like traditional disk “striping”, whereby the kernel uses both swap spaces in parallel.
Another way of increasing performance is to seperate the swap devices from data devices. If you have multiple hard disk controllers (or maybe a single SCSI host adapter with multiple channels, you can put the data on one channel and the hard disk with the swap service on a different channel. Putting the data on the SCSI devices and the swap device on an IDE controller may also be possible. In essence, you are trying to keep swap on the less used devices, so that the heavily used devices don’t get slowed down by swap. Also putting difference swap devices on different IDE controllers will increase throughput.
Let’s assume that we have four IDE hard disks. We might have something in our /etc/fstab that looks like this:
The priority for all of the first three swap partitions is 10. This means they all have the same priority and since Linux uses swap devices with a higher priority first, these three will be used before the last one. Note that in this case, the system will not wait until the first one is full before using the second one. Because the swap devices all have the same priority, Linux will parallelize the access. That means the the start of each swap partition will be used first, which has faster access than areas further out. Note that the devices hda2 and hdb2 are on seperate drives, but they use the same controller. This is not as efficient as using hda2 and hdc2, which are on seperate controllers. For more details take look at the mkswap command.
With the Linux 2.6 and later kernel, swap files are basically as efficient as swap devices. This is useful as swap files are easier to administer than swap devices. You can add, remove and resize then as you need to. If you have two hard disks, then you can create swap files on each devices (with the same priority) and improve swap performance. However, always keep in mind that swap space is just a safety mechanism. If you really need to swap often then the best thing is to buy more RAM.