Thursday, September 9, 2010

Create Swap How To

Swap Space


Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the physical memory is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

The size of your swap space should be equal to twice your computer's RAM. It also depends upon the application which you are deploying on the server.

Creating Temporary Swap Space using file

For example, to create a 32-MB swap file, you can use the command:

dd if=/dev/zero of=/swap bs=1024 count=32768

This will write 32768 blocks (32 MB) of data from /dev/zero to the file /swap. (/dev/zero is a special device in which read operations always return null bytes. It's something like the inverse of /dev/null.) Once you have created the swap file or partition, you can use the mkswap command to "format" the swap area.

mkswap -c /swap 8192

After running mkswap on a swap file, use the sync command to ensure the format information has been physically written to the new swap file. Running sync is not necessary when formatting a swap partition.

If you are using a swap file (and not a swap partition), you need to change its permissions first, like this:

chmod 0600 /swap

In order for the new swap space to be utilized, you must enable it with the swapon command.

swapon /swap

When you don't need the swap space any more, remove it with the following statements:

swapoff /swap rm /swap

Creating a Swap Partition

Default partition type is LINUX native, to change a Linux partition to a swap partition with fdisk:

l # show partition

t 1 # change partition type for partition 1

Hex code: 82 # set type to Linux swap

w # write partition table

The new swap partion is set using the below listed commands to create and enable the swap partition.

mkswap -c /dev/hda2 size_in_blocks

swapon /dev/hda

To enable all swap partitions:

swapon -a

Check swap space usage with /usr/bin/top, /usr/bin/free and defined partitions with /sbin/swapon as follows:

/sbin/swapon -s

Filename Type Size Used Priority /dev/hda5 partition 2048248 0 -1 /dev/hda6 partition 2048248 0 -2

The Linux memory manager limits the size of each swap space to 2 GB. You can, however, use up to 8 swap spaces simultaneously, for a total of 16GB.

Note that this document comes without warranty of any kind. But every effort has been made to provide the information as accurate as possible. I welcome emails from any readers with comments, suggestions, and corrections at webmaster_at admin@linuxhowto.in

Copyright © 2012 LINUXHOWTO.IN

No comments:

Post a Comment