Adding swap space in Linux

So, you've just bought a new stick of RAM for your computer and want to extract the best performance from your newly upgraded system? Everyone knows that more is better when it comes to RAM, and this is true for swap space, too.

Swap space is used by Linux to increase the available RAM in your system. An area of your hard drive is allocated as swap space during Linux installation. This space is usually a partition on your hard drive which can not be increased in size when a hard drive has been fully partitioned, which is often the case. How do you add more swap space to your system? The answer is to use a swap file.

A swap file is identical to a swap partition in content, the only difference is that a swap file appears as a file on your hard drive instead of being effectively invisible to the user. Using a swap file is potentially less efficient than a swap partition, as the hard drive on which you create the file may already be fragmented from creating and deleting many files in the past. This will make a swap file perform slightly more slowly than a swap partition, but in most cases the difference will be negligible.

Creating a swap file is not a hard process, but you do need to know how big a swap file you want to create. Linux requires that you have a 2:1 ratio of swap to physical RAM in your system for optimal performance. On my system with 256MB of physical RAM, this would mean I need 512MB of swap space. To find out how much physical RAM and swap space, type in a shell:

$ cat /proc/meminfo

The important lines from this output are:

MemTotal: 262144 KB
SwapTotal: 122880 KB.

This tells me that my system has 256MB of physical RAM (divide the size by 1024) and 120MB of swap. For optimal performance I will need to add 392MB of swap space to my system.

Next, you will need to find a drive that has enough available space to store the swap file. This can be found by typing:

$ df

From the following output:

Filesystem    1k-blocks    Used    Available    Use%    Mounted on   
/dev/hda1    497667    268480    203485    57%    /   
/dev/hda2    1981000    1583240    295346    85%    /usr   
/dev/sda1    4127204    3419812    497740    88%    /sda1   

It is apparent that /sda1 has enough space. The final step is "to create and initialise the swap file on this drive. To do this, you will need to be logged in as the root user. First, we must create the swap file. The size of the swap file is determined by multiplying the size in megabytes by 1024; in my case, this gives 401408. The swap file can be created by typing:

# dd bs=1024 count=401408 if=/dev/zero of=/sda1/swap

After some time, this will create the swap file. Now we need to mark the file as a swap file. To do this, type:

# mkswap /sda1/swap

Next we need to test if the file works. As root, type:

# swapon /sda1/swap

Now, by checking the system memory (as we have already done), we should see a change.

$ cat /proc/meminfo

SwapTotal: 524288 kB

The swap file works! The final step is to add the swap file to /etc/fstab so that it is initialised on boot like your other swap space. As root in your favourite text editor, open the file /etc/fstab and add the following:

/sda1/swap none swap sw 0 0

Upon reboot, your system should now start with the new swap file initialised automatically. New versions of Linux (kernel 2.4) can support up to 32 swap files. Older versions (kernel 2.0, 2.2) may be limited to eight swap files.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Alastair Cousins

PC World

Comments

Comments are now closed.

Latest News Articles

Most Popular Articles

Follow Us

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Resources

Best Deals on GoodGearGuide

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?