Linux 2.0.30, Swap Space and You


REFERENCE: man swapon, man mkswap, man fstab

What is Swap

If the world were a kind and just place you would not have to know what swap is. Unfortunately, it isn't and you do. Swap is sort of a scratch disk on UNIX systems. When you install the OS and partition your disk drive you usually designate some area as "swap". The OS will use this area and many applications will use this area without you ever realizing it. The rule of thumb is to make the swap size twice as big as your memory size. So if you have 64 MB of RAM, have at least 128 MB of swap.

I wrote the above for Solaris, under Linux is pretty much the same. Swap is used as a last resort by Linux. You may see your available memory almost always near 0 in Linux, but rarely see the swap getting used. Linux uses available memory as disk cache. As it needs more memory for other things it gives up some disk cache to make room. Using swap is slow, so Linux tries pretty hard not to use it. Nonetheless, you should still have some.

There are some useful commands under Solaris for seeing how much swap you have, where it is, and how its being used. The only one I can find for Linux is "top".

Running top will tell you how much swap is available and how much is being used. Usually it will be that none is being used. This is good.

If your system says that you have zero swap available, but you think that there should be some available, this is what you should check.

1) Make sure you set up a swap partition or file. Usually you make a swap partition when you set up the system. You can check your partition table using fdisk (as root). Run fdisk /dev/sda or fdisk /dev/hda, etc. and have it print out the partition table. You should have one partition labeled "linux swap".

2) If you don't have a partition labeled Linux Swap and you want to make some swap (or you need more swap) without repartitioning your hard drive (and reformatting and losting all data) you can still do it. I haven't done it yet but I ran across this in the man page for mkswap.

       mkswap  can  also set up swap files, although the file has
       to be created first.  A sequence of  commands  similar  to
       the following is reasonable for this purpose:

              # dd if=/dev/zero of=swapfile bs=1024 count=8192
              # mkswap swapfile 8192
              # sync
              # swapon swapfile

       Note  that  the regular file has to be created before run-
       ning mkswap on the file, and that the file must  not  con-
       tain  any holes (so, using cp(1) to create the file is not
       acceptable).

3) After you make your partition, or verify its existence, or make your new swap file, you may need to run mkswap on that partition or file. Note that the second command above for making swap space out of a file is "mkswap". To do this for your partition, just say. (see man mkswap for more details)

     mkswap /dev/sda2 (or /dev/hda2 or whatever the device is)
4) Add an entry in /etc/fstab. It should look like this.

     /dev/sda2       none            swap    sw  

Do a man fstab for more details.

5) Then do /sbin/swapon -a and it should add all swap found in your fstab. Run top to verify that it is available. If you want to make sure that your swap is added when you reboot, you need something like what is shown below in one of your boot scripts (/etc/rc.d/*) I have mine in rc.S


	echo "Trying to enable swap with /sbin/swapon -a"
	/sbin/swapon -a

That's it.

Return to Gene's Home Page
Return to Gene's Random Unix Crap