Adding swap space

Swap Memory is a space in the Hard Disk of your computer that Operating Systems (Linux in our case) will use to put the info that is actually on the RAM to free it for another application. Sometimes in the course of a system’s existence you find that the swap partition you set up at install-time just isn’t enough anymore. So we need to increase the swap space for better performance of the system. You have two options: add a swap partition or add a swap file. It is recommended that you add a swap partition, but sometimes that is not easy if you do not have any free space available in the disk to create partition.

A) To add a swap partition (assuming /dev/sdb2 is the swap partition you want to add):

1. The hard drive can not be in use (partitions can not be mounted, and swap space can not be enabled). The easiest way to achieve this it to boot your system in rescue mode. Refer to Chapter 8 for instructions on booting into rescue mode. When prompted to mount the filesystem, select Skip.

Alternately, if the drive does not contain any partitions in use, you can unmount them and turn off all the swap space on the hard drive with the swapoff command.

2. Create the swap partition using parted or fdisk. Using parted is easier than fdisk; thus, only parted will be explained. To create a swap partition with parted:

* At a shell prompt as root, type the command parted /dev/sdb, where /dev/sdb is the device name for the hard drive with free space.

* At the (parted) prompt, type print to view the existing partitions and the amount of free space. The start and end values are in megabytes. Determine how much free space is on the hard drive and how much you want to allocate for a new swap partition.

* At the (parted) prompt, type mkpartfs part-type linux-swap start end, where part-type is one of primary, extended, or logical, start is the starting point of the partition, and end is the end point of the partition.

Changes take place immediately; be careful when you type.

* Exit parted by typing quit.

3. Now that you have the swap partition, use the command mkswap to setup the swap partition. At a shell prompt as root, type the following:

mkswap /dev/sdb2

4. To enable the swap partition immediately, type the following command:

swapon /dev/sdb2

5. To enable it at boot time, edit /etc/fstab to include:

/dev/sdb2 swap swap defaults 0 0

The next time the system boots, it will enable the new swap partition.

6. After adding the new swap partition and enabling it, make sure it is enabled by viewing the output of the command cat /proc/swaps or free.

B) To add a swap file:

1. Determine the size of the new swap file and multiple by 1024 to determine the block size. For example, the block size of a 128 MB swap file is 131072.

2. At a shell prompt as root, type the following command with count being equal to the desired block size:

root@localhost ~> dd if=/dev/zero of=swapfile bs=1024 count=131072
131072+0 records in
131072+0 records out


dd: – is used to copy a specified number of bytes from an Input file (if) to an Output file (of). Here the dd command copy null characters from the special file “/dev/zero” and copy it to the output file “aSwapFile” in the “/” directory. The “bs” specifies that the characters are read as BYTES. The “count” specifies the size of the bytes block that is to be created in the output file.

3. Change the permission

root@localhost ~> chmod 600 swapfile

4. Setup the swap file with the command

root@localhost ~> mkswap swapfile
Setting up swapspace version 1, size = 135372800 bytes

5. To enable the swap file immediately but not automatically at boot time:

root@localhost ~> swapon swapfile

6. To enable it at boot time, edit /etc/fstab to include:

/swapfile swap swap defaults 0 0

The next time the system boots, it will enable the new swap file.

7. After adding the new swap file and enabling it, make sure it is enabled by viewing the output of the command cat /proc/swaps or free.

Adding a Swap File on FreeBSD

1. Be certain that your kernel configuration includes the memory disk driver (md(4)). It is default in GENERIC kernel.

device md # Memory “disks”

2. Create a swapfile (/usr/swap0):

# dd if=/dev/zero of=/usr/swap0 bs=1024k count=64

3. Set proper permissions on (/usr/swap0):

# chmod 0600 /usr/swap0

4. Enable the swap file in /etc/rc.conf:

swapfile=”/usr/swap0″ # Set to name of swapfile if aux swapfile desired.

5. Reboot the machine or to enable the swap file immediately, type:

# mdconfig -a -t vnode -f /usr/swap0 -u 0 && swapon /dev/md0

Adding a Swap File on Solaris

If you have a Solaris system that badly needs more swap but you don’t have a free swap device available, you can create a file in the file system and add it as additional swap.

  1. Use mkfile to create a file suitable for a local swap area. For example, to create a 1GB swap file:
        /usr/sbin/mkfile 1024m /swap

    where /swap is the name of the file to be used as swap space. Units for the size can be kilobytes (k), blocks (b), or megabytes (m).

  2. Tell the system to start using the file as swap:
        /usr/sbin/swap -a /swap

    Use swap -l to verify that the swap file has been activated.

One response to “Adding swap space

  1. Wish I could get this working on my vps. But I think that side of things is limited. Any idea how i could go about increasing some sort of swap space for a debian vps with 512mb ram and running plesk? My services keep maxing out my memory when i know it shouldn’t run so high all the time.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s