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

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

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.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?