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/swapAfter 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.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Alastair Cousins

PC World
Show Comments

Brand Post

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Luke Hill

MSI GT75 TITAN

I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it.

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Louise Coady

Brother MFC-L9570CDW Multifunction Printer

The printer was convenient, produced clear and vibrant images and was very easy to use

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?