Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
Let The Music Play: Join EFF Today

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents
Up to --> Installing and Upgrading

· Getting Your Own Copy
· Preparing for the Installation
· Installation Checklist
· Hardware Requirements
· Partitioning
· Swap Space
· Installation Problems
· Preparing for the Worst

Glossary
MoreInfo
Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
FAQ
Copyright Info
Terms of Use
Privacy Info
Disclaimer
WorkBoard
Thanks
Donations
Advertising
Masthead / Impressum
Your Account

Communication
Feedback
Forums
Private Messages
Recommend Us
Surveys

Features
HOWTOs
News
News Archive
Submit News
Topics
User Articles
Web Links

Google
Google


The Web
linux-tutorial.info

Who's Online
There are currently, 164 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

  
Linux Tutorial - Installing and Upgrading - Preparing for the Installation - Swap Space
  Partitioning ---- Installation Problems  


Swap Space

Swap Basics

An older rule of thumb was to have a swap space at least the size of physical RAM, if not twice as large. However, you also need to consider growth. If you expect to increase your RAM in the future, you should consider that when you set how much space you are going to use for swap. RAM just slides into your system; increasing swap may require reinstalling the operating system, particularly if you have an older version of Linux.

So, how much do you assign to swap? Good question. In general, I still support the suggestion is twice as much as RAM. This is the "good reason" I mentioned for having more swap than physical RAM. Creating a large swap space is easier to do it now and waste the space than to reinstall later. Another good reason is when you have more than one user running graphical applications. In this case, then even setting swap to two times RAM is reasonable. If all the space is taken up on the primary hard disk, you can add a hard disk and use the swap command to add additional swap space.

The key factor is how many applications you will run and what kind of applications. You need to consider the behavior of the application. If you are doing work with some kind of graphics (i.e. graphic design, ray tracing, and so forth), then you might need to conside more swap. If you have a lot of users on your system, you might want more swap, as well.

Keep also in mind that accessing swap is slower than accessing RAM. You might want to consider adding more RAM. I have a co-worker who has 3 GB of RAM in his system as he does a lot of graphical work and it is an extreme burden on his system to be constantly swapping in and out. He never gets close to using all of his RAM, so he does not need a large swap space.

Versions of the Linux kernel prior to 2.4.10 "liked" to have at least twice as much swap as RAM. However, with the newer kernels, this is no longer true.

One important thing to consider is what happens when the system runs out of memory. Being out of memory (OOM) is a dangerous thing and can cause the whole system to come to a stand-still or even crash. Within the Linux memory management functions is something called and OOM killer which will stop (or kill) processes when the system runs out of memory. The problem is that currently (Sep 2004), processes are killed "blindly". That is, without regard to their importance. In general, the system calculates how "bad" the process is longer a process runs and the more memory it uses, the greater the chance is that it will be killed. (For details look at the file /usr/src/linux/mm/oom_kill.c).

The biggest problem is that it arbitrary. That is a program could be running correctly, not causing any problems, but it just runs a lot and uses a lot of RAM. Imagine if this was an database. The argument whether the database should be killed or the whole system stops is philosophical, so we won't go into it here. With 40 GB being a small hard disk today, allocating 1 GB (i.e. 2.5%) for peice of mind is not a bad idea.

You also need to keep in mind that swapping takes up system resources. The time to access the hard disk is hundreds of times slower than the time to access RAM. Therefore, if speed is an important consideration, you should think about having enough RAM so you don't swap. The maximum size of your swap space depends on your hardware architecture and more recent kernels on the i386 can have swap partitions that are as large as 2Gb and you can have as many as 8 different swap partitions if you have kernel older than 2.4.10. Later versions support up to 32 swap spaces.

Note that I said swap spaces and not just swap device or swap partition. Linux allows you to create a swap file. Like any other file, a swap file exists on your filesystem and takes up space. The advantage is that you can add a swap file at any time, provide you have the space on the hard disk. You don't need to to repartition your hard disk or even reboot.

There are two different swap versions (or formats). Kernels prior 2.4 supports only version 0 swap spaces. Versions later than Linux 2.1.117 support version 0 and version swap. However, Linux 2.5 only supports version 1. Therefore you need to be careful when upgrading. The mkswap command can format in either format. See the mkswap for more details.

Another change with the 2.4.10 kernel is that the swap spaces can be up to 64 Gb in size. Note, however, that with some Linux distributions, the mkswap command can currently (Sep 2003) only create swap devices that are 2GB or smaller.

Managing Swap

In many cases, once the system is installed, you never have to think about swap again. However, when you start using your system more actively, add new software, and so on, you will probably find that you should at least take a look at your current swap usage.

Linux provides a number of tools to monitor swap. The easiest is the free command, which gives you a quick overview of the memory usage. You can also use the top which can provide an self-updating view of your system, including memory usage by process, users on the system, and so forth. Also the /proc/swaps and /proc/meminfo files contain information about memory usage on your system.

Linux also provides tools to manage your swap space. You can add and remove spaces as you need to, as well as turn them on and off, even while they are being used.

To create a file to be used a swap, you need to first create the file. This is most easily done with the dd command. To create a 65 Mb file, you might have this command (from the the mkswap man-page):

which displays:

65536+0 records in 65536+0 records out

Next you have to prepare the file for usage as swap space using the mkswap. The simplest form would be:

mkswap device size

Where "device" is either the name of a device node for a hard disk partition or the name of a file you want to use. The "size" option is only required when you create a swap file. However, it is actually superfluous and still maintained for backwards compatibility. The command you issue migt look like this:

Which displays:
Setting up swapspace version 1, size = 67104 kB
What that this does is format the swap space by adding the swap header information. Note this said that it created a version 1 swap space. We could have used the -v0 option to create a version 0 swap space if we needed to.

One key thing to keep in mind is that the kernel needs to have the swap file created to it's full size before it is used. That is, it cannot be a sparse file (one that only uses part of the allocated space).

At this point we are ready to activate the swap space. If you are adding the swap space permanently, then you will need to include it in your /etc/fstab file. My default (initial) swap space looks like this:

/dev/hda5 swap swap pri=42 0 0

This basically says that the device /dev/hda5 is to be mounted onto the special mount point swap, is of type swap, has a priority of 42 and that the filesystem should not be dumped if the system crashes, nor should the filesystem be checked on system boot.

To automatically use the swap file we just created, we might add an entry that looks like this:

/data/swapfile.1 none swap pri=5,defaults 0 0

When your system boots, all swap devices will be added which are listed in the /etc/fstab, unless they have the "noauto" option (just like any normal filesystem). If I wanted to immediate add the swap space without having to reboot, I can run swapon -a, which will activate all swap spaces in the /etc/fstab file(again, unless they have the "noauto" option). If the swap space is already in use, the system will silently ignore it.

As you might guess, the priority of the swap space determines the order in which the swap space is used. The higher the priority the sooner it will be used. In this example, the primary swap space in its own parition has a priority of 42 and will be used before the swap file with a priority of 5.

We can also add swap space dynamically using the swapon command. After creating the swap space, you might activate it with this command:

To show what is currently beeing used as swap space we issue the command

This might show us:

Filename Type Size Used Priority /dev/hda5 partition 409616 200560 42 /data_c2/swapfile.1 file 65528 0 -1

Just as you can enable swap from the command line, you can also turn it off. This is done with the swapoff command and it might look like ths:

For more details see the swapoff man-page.

If performance is really an issue thare are number of different things you can do. If you have multiple hard disk on different controllers, you can put swap spaces on each device and give them the same priority. This works like tradition disk "striping", whereby the kernel uses both swap spaces in parallel.

Another way of increasing performance is to seperate the swap service from data device. If you have multiple hard disk controllers (or maybe a single SCSI host adapter with multiple channels, you can put the data on one channel and the hard disk with the swap service on a different channel. Putting the data on the SCSI devices and the swap device on an IDE controller may also be possible. In essence you are trying to keep swap on the less used devices, so that the heavily used devices don't get slowed down by swap. Also putting difference swap devices on different IDE controllers will increase throughput.

Let's assume that we have four IDE hard disks. We might have something in our /etc/fstab that looks like this:

/dev/hda2 none swap sw,pri=10 0 0 /dev/hdb2 none swap sw,pri=10 0 0 /dev/hdc2 none swap sw,pri=10 0 0 /dev/hdd2 none swap sw,pri=1 0 0

The priority for all of the first three swap partitions is 10. This means they all have the same priority and since Linux uses swap devices with a higher priority first, these three will be used before the last one. Note that in this case, the system will not wait until the first one is full before using the second one. Because the swap devices all have the same priority, Linux will parallelize the access. That means the the start of each swap partition will be used first, which has faster access than areas further out. Note that the devices hda2 and hdb2 are on seperate drives, but they use the same controller. This is not as efficient as using hda2 and hdc2, which are on seperate controllers. For more details take look at the mkswap command.

With the Linux 2.5 and later kernel, swap files are basically as efficient as swap devices. This is usefil as swap files are easier to administer than swap devices. You can add, remove and resize then as you need to. If you have two hard disks, then you can create swap files on each devices (with the same priority) and improve swap performance. However, always keep in mind that swap space is just a safety. If you really need to swap often then the best thing is to buy more RAM.

 Previous Page
Partitioning
  Back to Top
Table of Contents
Next Page 
Installation Problems


MoreInfo

Test Your Knowledge

User Comments:


You can only add comments if you are logged in.

Copyright 2002-2009 by James Mohr. Licensed under modified GNU Free Documentation License (Portions of this material originally published by Prentice Hall, Pearson Education, Inc). See here for details. All rights reserved.
  
Show your Support for the Linux Tutorial

Purchase one of the products from our new online shop. For each product you purchase, the Linux Tutorial gets a portion of the proceeds to help keep us going.


Login
Nickname

Password

Security Code
Security Code
Type Security Code


Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!


Amazon Wish List

Did You Know?
You can help in many different ways.


Friends



Tell a Friend About Us

Bookmark and Share



Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.10 Seconds