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 PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

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

Alastair Cousins

PC World
Show Comments


Microsoft L5V-00027 Sculpt Ergonomic Keyboard Desktop

Learn more >

Lexar® JumpDrive® S57 USB 3.0 flash drive

Learn more >


Lexar® JumpDrive® S45 USB 3.0 flash drive 

Learn more >


Lexar® JumpDrive® C20c USB Type-C flash drive 

Learn more >

Audio-Technica ATH-ANC70 Noise Cancelling Headphones

Learn more >

HD Pan/Tilt Wi-Fi Camera with Night Vision NC450

Learn more >

Lexar® Professional 1800x microSDHC™/microSDXC™ UHS-II cards 

Learn more >


Back To Business Guide

Click for more ›

Most Popular Reviews

Latest News Articles


GGG Evaluation Team

Michael Hargreaves

Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Kathy Cassidy


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

Anthony Grifoni


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

Steph Mundell


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


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.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?