LFCS - Working With Swap Memory

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
372
Reaction score
432
Credits
13,798
Some people may work on systems that do not have enough Random Access Memory (RAM) to run all the programs you want to be running. To manage this, you can have a swap file or swap partition.

The way it works is that unused areas, or chunks, of memory are moved from RAM onto the swap media. Once moved, there is more RAM available for executing programs. There is nothing you need to do to free up memory, it is managed by the kernel. What you can control is where the swap is located and its size.

Creating a Swap Partition

During the setup of Linux, you should be asked about setting up partitions. Some installations, depending on your hardware, may setup a swap partition.

To see what media you are using for swap, use the command:

Code:
swapon -s

The command 'swapon' is used to manage swap. The parameter '-s' will list a summary of your swap media.

For CentOS, the swap defaults to '/dev/dm-1' and for Ubuntu, it defaults to '/swapfile'. Obviously, Ubuntu is using a swap file and not a partition. CentOS is using a device called 'dm-1'. We can look that there is also a 'dm-0' by using the command 'lvdisplay'. The 'dm-0' is your ROOT Logical Volume and 'dm-1', most times, is the swap partition. Using the command, we can see in Figure 1 the swap file in CentOS.

Figure 1.JPG

FIGURE 1

NOTE:
To use the command 'lvdisplay' on Ubuntu, you need to install the package 'lvm2'. Keep in mind that Ubuntu doesn't use the 'dm-0' or 'dm-1', so nothing is shown when using the command.

So, in my case, CentOS is using a partition of 2.5 GB for a swap file. Hopefully, you can see a minor issue, but not a major issue unless your system has a low amount of RAM and you'll be using your swap a lot.

The swap partition is on the same physical disk as your Operating System (OS). So, as you are accessing OS files and the swap is being used, things will slow down as the swap is being managed. The OS files cannot be accessed as fast if the swap is also sharing the same read and write heads of the hard disk, or whatever media type you are using. It can be best to put the swap on another physical media so a single drive is not having to share time to read and write on the same media. You can have the same issue with a swap file that is on the same media as the OS, as we see in Ubuntu.

So, let's look at setting up a partition first, one that is not on the same media as the OS. If you have been following along with setting up a virtual system in VirtualBox, we will move the swap media to '/dev/sdb8'.

To mark the partition as a swap partition, we can use the command:

Code:
mkswap /dev/sdb8

Change the partition as you need if 'sdb8' will not be appropriate for your system.

We can check the partition with the command:

Code:
lsblk /dev/sdb

Partition 'sdb8' is not listed as swap yet because it is not activated. To activate the swap partition, we can turn it on with the command:

Code:
swapon /dev/sdb8

Now, if you run 'lsblk /dev/sdb', you can see that the partition 'sdb8' is labeled as '[SWAP]'. We can further verify that the swap partition is being used with the command (Figure 2):

Code:
swapon -s

Figure 2.JPG

FIGURE 2

We can also use the command 'free -m' to show physical RAM and swap. If I do this, I can see that the original 2.5 GB now has an additional 1 GB from the swap partition and has a total swap of 3.5 GB, as you can see in Figure 3.

Figure 3.JPG

FIGURE 3

We now have a temporary swap partition. The commands will work in both CentOS and Ubuntu. If we reboot the system, the swap partition we added disappears. To make this permanent, we need to load the partition in the '/etc/fstab' file. We'll get to that soon.

You may ask, but what about performance on the OS media if the original swap is still there? Let's temporarily turn that off so you can see how to use the commands to turn off the swap and turn it on.

If you use the command 'swapon -s' to see the name of the swap media. On CentOS, it is '/dev/dm-1' and Ubuntu it is '/swapfile'. So use the following command with the swap name given:

Code:
swapoff <swap-name>

To list the swap media again, 'swapon -s', to see that the original swap is not turned off.

NOTE: You need ROOT privileges to run 'swapoff', so use 'sudo su' or 'sudo <command>'.

Now, using 'swapon' or 'swapoff' you can turn a specific swap on or off, whether a file or partition.

With no need to reboot, you can undo the changes by turning off all swap media and then turning it all back on. When the swap is turned back on, it defaults to what is listed in the '/etc/fstab' file. To undo the changes, do the following two commands:

Code:
swapoff -a
swapon -a

The '-a' parameter is for 'all' swap media, whether partition or file.

Swap Priority

When a swap media is added, it is given a priority. The priority only has meaning if you have more than one swap listed. See Figure 4 for an example.

Figure 4.JPG

FIGURE 4

This example is taken from a CentOS system, but works exactly the same on Ubuntu. The higher the number, the higher the priority. Higher priority media is used first. So, in the example, the swap on '/dev/dm-1' is filled before 'dev'sdb8' is used. If two swap media spots have the same priority, then the kernel will use one for moving data to, then the next one, doing this back and forth until one is full. The remaining media with the same priority is used until full. Going back and forth is known as the 'round robin' method. Keep in mind that there can be over two swaps with the same priority.

When you use 'swapon' to enable swap, you can specify the priority with the parameter '-p <priority>'. The priority value can be any number from -1 to 32767. If you have a priority already set on a device at -2, then you can add a device at a priority of -3, or one less than the lowest priority.

Swap File

To use a swap file is a little different, but not much. The steps to create the swap file is:
  1. Create the file of the proper size
  2. Set the permissions on the file
  3. Mark the file as a swap file
  4. Turn on the swap
So, we need to create the file first. To do this, we use the 'dd' command. In the example, we'll create a file with a size of 2 GB (2048) and create it setting the blocks to a size of 1 MB and write the file with just zeroes.

Code:
dd if=/dev/zero of=/swapfile count=2048 bs=1MiB

We are putting the file in the root folder, but you can move this. In Ubuntu, the 'swapfile' already existed, so I changed the name to 'swapfile1'. You can name the files any allowed file name.

After the file is created, you need to set the permissions to allow read and write permissions for ROOT. Since the kernel is managing the swap, ROOT is all that needs permission for it. Use the following command:

Code:
sudo chmod 600 /swapfile

Change the location and filename as needed.

Now, we have the file and it has the proper permissions. We need to mark the file as swap:

Code:
sudo mkswap /swapfile

The resulting output should look similar to Figure 2. The file is given a UUID, which you will not need to later. This UUID cannot be used to add to the file to '/etc/fstab' if you want the swap file to be loaded at boot. The UUID can only be used for partitions and not files.

The last step is to turn on the swap. This is the same as we did to start a partition by using the 'swapon' command. You can also set the priority as you need if needed.

Code:
swapon /swapfile

You can check the status of the swap with 'swapon -s' and see the total swap memory with 'free -m'.

FileSystem Table

If you want the swap loaded at boot, then you need to update the FileSystem Table (fstab).

What you need for partitions is the UUID which you can use the following command to list the UUIDs of partitions:

Code:
lsblk --output NAME,SIZE,TYPE,UUID

Once you have the UUID of a swap partition, you need to edit '/etc/fstab' with elevated privileges.

Add a line for your partition at the end of the file. It should resemble the following:

Code:
UUID=<UUID given in blkid command> swap swap sw 0 0

If you want to specify a priority, then it is added with the parameter 'sw' as follows:

Code:
UUID=<UUID given in blkid command> swap swap sw,pri=5 0 0

Here, the priority is set to '5', but you can make it any valid number.

When you want to add a file and not a partition, you replace the UUID with the path to the file:

Code:
/swapfile swap swap sw 0 0

You can set the priority on the file the same way as you do a partition. All you need to do is reboot and check the 'swapon -s' output.

An example of my fstab for the swap file and partition is:

Code:
/swapfile1 swap swap sw 0 0
UUID="faffe479-7414-4ee6-81ed-39933715b601" swap swap sw 0 0

Conclusion

Setting up swap on a Linux system can be beneficial for a system that has low RAM or will run a lot of programs or services that could use up the amount of RAM in the system.

Even on some systems with enough RAM, it can be useful just to have a swap available with a large strain on the RAM. Servers especially can have a strain put on them if the amount of users using it gets large.
 

Members online


Top