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
So, how much do you assign to swap? Good question. In general, I still
support the suggestion is twice as much as
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
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{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}) for peice of mind is not a bad idea.
You also need to keep in mind that
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
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
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
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
which displays:
Next you have to prepare the file for usage as swap space using
the
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
This basically says that the device
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
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
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
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
Let’s assume that we have four IDE hard disks. We might have something in our
/
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
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.