Managing Swap Space in Linux

A swap space is an area of a disk which can be used with the Linux kernel memory management subsystem. Swap spaces are used to supplement the system RAM by holding inactive pages of memory. The combined system RAM plus swap space is called virtual memory. When the memory usage in a system exceeds a defined limit, the kernel will comb through RAM looking for idle memory pages assigned to processes. The kernel will write the idle page to the swap area, and will reassign the RAM page to be used by another process. If a program requires access to page that has been written to disk, the kernel will locate another idle page of memory, write it to disk, then recall the needed page from the swap area. Since swap areas reside on a disk, swap is incredibly slow compared to RAM. While it is used to augment system RAM, usage of swap spaces should be kept to a minimum whenever possible.

 

Create a swap space

To create a swap space, an administrator need to do three things:

1) Create a partition

2) Set the type of the partition as 82 Linux swap

3) Format a swap signature on the device.

Use a tool, such as fdisk, to create a partition of the desired size.

$ fdisk /dev/vdb

 

Assign the partition type

After the swap partition, has been created, it is recommended to change the partition’s type, or system ID, to 82 Linux swap. The partition type is not used by utilities any longer, having the type set allows administrators to quickly determine the partitions purpose

 

Format the device

The mkswap command applies a swap signature to the device. Unlike other formatting utilities, mkswap writes a single block of data at the beginning of the device, leaving the rest of the device unformatted so it can be used for storing memory pages.

$ mkswap /dev/vdb1

 

Activate a swap space

An administrator can use the swapon command to activate a formatted swap space. Swapon can be called on the device, or swapon -a will activate all swap spaces listed in the /etc/fstab file.

$ free

total      used       free       shared    buffers     cached

Mem:       595        482        112         0              63            324

-/+ buffers/cache:   93        501

 

$ swapon /dev/vdb1

$ free

total      used       free       shared    buffers     cachedMem:       595        482        112          0             63             324-/+ buffers/cache:  93        501

Swap:         240         0          240

 

Persistently activate swap space

It is likely that a swap space will be required to automatically activate every time the machine boots. In order for the machine to activate the swap space at every boot, it must be configured in the /etc/fstab file. If needed an administrator can deactivate a swap space using the swapoff command. A swapoff will only be successful if any swapped data can be written to other active swap spaces or back into memory. If data cannot be written to other places, the swapoff will fail, with an error, and the swap space will stay active.

The following is an example line in /etc/fstab adding a previously created swap space.

UUID=fdb7fa60-b781-44a8-961b-37ac3ef572bf  swap swap  defaults  0  0

The example uses the UUID as the first field. The UUID is stored in the swap signature stored on the device, and was part of the output of mkswap. If the output of mkswap has been lost, the blkid command can be used to scan the system and report on all attached block devices. The raw device name can also be used in the first field. The second field is typically reserved for the mount point. However, for swap devices, which are not accessible through the directory structure, this field is the placeholder value swap. The third filed is the file system type. The file system type for a swap space is swap. The fourth field is for options. In the example, the option defaults is used. Defaults includes the mount option auto, which is what causes the swap space to be automatically activated at boot. The final two fields are the dump flag and fsck order. Swap spaces require neither backing up nor file system checking.

 

If you need any further assistance please contact our support department.

Was this answer helpful? 0 Users Found This Useful (0 Votes)