Linux Swap Memory: Management and Configuration

To manage temporary data, Linux Swap Memory reduces the use of physical memory or RAM. Thus, creating an amount of virtual memory, allows applications to consume more amount of installed memory. In the following article, we will show three ways to configure swap memory to support the management of our customers’ applications.

If you want to find out more about Linux configurations, read: Parse Everything you Want Using the Linux Shell

Linux Swap Memory creation from a text file

This a simple method that doesn’t require experience because you don’t need to change the partitions and take the space needed from the disk using the dd command.

Example:

Put 2 GB of swap memory to extend the memory existence to 4 GB.

The first step is to check the swap memory on your server using the next free command:

[root@intraway:~]# free –m

It shows something similar to this:

Linux Swap Memory

After that, use the dd command writing, as a root user, the next sentence:

[root@intraway:~]# dd if=/dev/zero of=/var/swap bs=1024 count=2048000

It may take some minutes. Then is necessary to give a swap memory format to the file, using command mkswap like this:

[root@intraway:~]# mkswap /var/swap

The result should be:

Linux Swap Memory

Then, is necessary to activate the new partitions by using swapon command:

[root@intraway:~]# swapon /var/swap

To check the procedure use the free command:

[root@intraway:~]# free –m

The results should be:

Linux Swap Memory

Creation of swap memory from LVM

To do this procedure is important to have enough space in the volume group (VG) of the disk. If the server doesn’t have enough space is necessary to add another disk. To do the verifications use the next free command:

[root@intraway:~]# free -g

Linux Swap Memory

To explain the procedure in this specific case, I will present a disk on a virtual machine. To that meaning is important to certify if the operative system recognizes the new disk using the fdisk command:

[root@intraway:~]# fdisk -l /dev/sdb

It will show something like this:

Linux Swap Memory

Then check the disk volume group using the command:

[root@intraway:~]# pvs

The following will appear:

Linux Swap Memory

Then, using the pvcreate command, mark the disk as physical volume:

[root@intraway:~]# pvcreate /dev/sdb

It will present:

Linux Swap Memory

Then, assign the disk to a logical volume using the lvs command to know all the options:

[root@intraway:~]# lvs

Something similar to this will appear:

Linux Swap Memory

Thir server has a logical volume for swap memory, so you need to extend the logical volume using the vgextend command:

[root@intraway:~]# vgextend VG_00 /dev/sdb

The result should be:

Linux Swap Memory

After that is necessary to assign the space from the new disk to the same logical volume using the Ivextend command:

[root@intraway:~]# lvextend -l 100%FREE /dev/VG_00/LogVol_swap

It will show:

Linux Swap Memory

To see the change is necessary to turn off the swap memory, apply the resize and, then turn on the swap memory:

[root@intraway:~]# swapoff /dev/VG_00/LogVol_swap[root@intraway:~]# mkswap -f /dev/VG_00/LogVol_swap

Linux Swap Memory

[root@intraway:~]# swapon /dev/VG_00/LogVol_swap

Remember to check the change:

[root@intraway:~]# free –g

Linux Swap Memory

Other options to use a part of the disk

Use the part of the disk a left of the rest to another file system. Using the possibility to partition the disk with the command:

[root@intraway:~]# fdisk /dev/sdb

It will pop up the next menu:

Linux Swap Memory

For the example, I’ve created 2 partitions selecting the option n:

Linux Swap Memory

The image contains the steps you have to follow:

  • Select p (primary partition)
  • The number of the partition 1
  • Select the position to start cylinder 1
  • Establish the space to the disk using: +(number of gb). In this case: +2 or +8 for a disk with 16 GB.

Make sure to use the option p. If you want to create another partition, do the same procedure, but remember that the number of the partition is 2 and the server takes the rest for defect.

Linux Swap Memory

Then use w to save the change, or Q if you are not sure and want to quit without safe.

Linux Swap Memory

After the creation of the partitions to configure the swap memory, apply the following commands:

  • Vgcreate vgswap /dev/sdx1
  • Lvcreate –n lvswap –L 2G /dev/mapper/vgswap-lvswap
  • Mkswap  /dev/mapper/vgswap-lvswap
  • Edit the /etc/fstab and left this way
    • /dev/mapper/vgswap-lvswap  swap    defaults        0 0

With the free command, check before and after to turn on the swap memory.

Before:

Linux Swap Memory

After:

Linux Swap Memory

To obtain a perfect performance from the applications in the operative system, the configuration must be optimal. It’s important to take care of aspects like memory, disk space, network interface, and kernel. This way you can be confident of the system efficiency where the applications are running.  

Keep learning! What is PEN?

You may also like

managing your tech team

6 Best Practices for Managing Your Tech Team

Linux

Distribuciones Linux Amigables

Linux

Linux Onboarding

Menu