Loading...
Basic Linux

Swap

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM 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, a swap file, or a combination of swap partitions and swap files.

Swap should equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB.

So, if:

M = Amount of RAM in GB, and S = Amount of swap in GB, then

If

M < 2 S = M *2

Else

S = M + 2

Using this formula, a system with 2 GB of physical RAM would have 4 GB of swap, while one with 3 GB of physical RAM would have 5 GB of swap. Creating a large swap space partition can be especially helpful if you plan to upgrade your RAM at a later time. For systems with really large amounts of RAM (more than 32 GB) you can likely get away with a smaller swap partition (around 1x, or less, of physical RAM).

There are two different types of swaps that you can have:
1) File Swap
2) Partition Swap

Partition Swap

  • Create a partition

#fdisk    /dev/sda

Update to kernel

#partprobe    /dev/sda

Use the mkswap command to create a swap space
Syntax:

mkswap  [options]  [Device]

Options
-c Checks the device for bad blocks before creating the swap area

#mkswap    /dev/sda8

Enable the swap partition

#swapon     /dev/sda8

Verify the swap is running correctly

#swapon   -s

Syntax:

swapon   [options]  [Device]

Options
-a Enables all swap device
-e silently skips devices that don’t exist
-s Verifies that the swap is running

If you want to turn off the swap, you can use the swapoff command.
Syntax:

swapoff   [options]    [device]

Options:
-a Enables all swap devices
-e Silently skips device that don’t exist
-s Verifies that the swap is running

File Swap

You can use the dd command to reserve space for another swap on the /dev/sda partition.
The dd command can be for many different purposes and has a huge syntax

Reserve 1GB of space of the swap.

#dd  if = /dev/zero      of = /mnt/file_swap     bs=1024   count=1000000

Just as with partition swaps, you now create a swap space specifying the file just created.

#mkswao   /mnt/file_swap

Enable the swap.

#swapon   /mnt/file_swap

Again you can verify that the swap is enabled

#swapon    -s

Note: The big difference between the two swap types is that file swap is easier to manage because you just move the swap file to another disk if you want. The swap partition would need to be removed, re-created and so on. Although RedHat recommends using a partition swap, File swap are fast enough these days with less administrative overhead to not use them instead. One word of caution though, is that you can use only one swap (of either type) per physical disk.

Leave a Reply

Your email address will not be published. Required fields are marked *