Difference between revisions of "Adding and Managing Ubuntu 11.04 Swap Space"

From Techotopia
Jump to: navigation, search
(New page: An important part of maintaining the performance of an Ubuntu system involves ensuring that adequate swap space is available relative to the memory demands placed on the system. The goal o...)
(No difference)

Revision as of 17:30, 22 June 2011

An important part of maintaining the performance of an Ubuntu system involves ensuring that adequate swap space is available relative to the memory demands placed on the system. The goal of this chapter, therefore, is to provide an overview of swap management on Ubuntu 11.04.

What is Swap Space?

Computer systems have a finite amount of physical memory that is made available to the operating system. When the operating system begins to approach the limit of the available memory it frees up space by writing memory pages to disk. When any of those pages are required by the operating system they are subsequently read back into memory. The area of the disk allocated for this task is referred to as swap space.

Identifying Current Swap Space Usage

The current amount of swap used by an Ubuntu system may be identified in a number of ways. One option is to cat the /proc/swaps file:

$ cat /proc/swaps
Filename            Type            Size    Used    Priority
/dev/sda5           partition       492540  0       -1

Alternatively, use the swapon command:

$ swapon -s
Filename             Type            Size    Used    Priority
/dev/sda5            partition       492540  0       -1

Finally, the free command may also be used:

$ free
             total       used       free     shared    buffers     cached
Mem:       1023064     447472     575592          0      21268     166108
-/+ buffers/cache:     260096     762968
Swap:       492540          0     492540

Adding a Swap File to an Ubuntu 11.04 System

Additional swap may be quickly added to the system by creating a file and assigning it as swap. This is achieved as follows.

Create the swap file using the dd command (the size can be changed by adjusting the count= variable; the following creates a 131MB file):

$ sudo dd if=/dev/zero of=/newswap bs=1024 count=128000
128000+0 records in
128000+0 records out
131072000 bytes (131 MB) copied, 1.7639 seconds, 74.3 MB/s

Configure the file as swap:

$ sudo mkswap /newswap
mkswap: /newswap: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 127996 KiB
no label, UUID=683aa598-4922-490d-ac39-01d5cc5b419e

Add the swap file to the system in real-time:

$ swapon /newswap

Finally, modify the /etc/fstab file to automatically add the new swap at system boot time by adding the following line:

/newswap  none  swap  sw  0 0

Once the swap space has been activated, verify that it is in use using the swapon –s command:

$ swapon -s
Filename              Type            Size    Used    Priority
/dev/sda5             partition       492540  0       -1
/newswap              file            127996  0       -2

De-activate the additional swap space at any time using the swapoff command as follows:

$ sudo swapoff /newswap