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!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Alastair Cousins

PC World
Show Comments


James Cook University - Master of Data Science Online Course

Learn more >


Sansai 6-Outlet Power Board + 4-Port USB Charging Station

Learn more >



Back To Business Guide

Click for more ›

Brand Post

Most Popular Reviews

Latest Articles


PCW Evaluation Team

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.

Walid Mikhael

Brother QL-820NWB Professional Label Printer

It’s easy to set up, it’s compact and quiet when printing and to top if off, the print quality is excellent. This is hands down the best printer I’ve used for printing labels.

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

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?