What is swap

Swapping technology allows you to store some amount of data (which does not fit in the system’s RAM) on the hard drive, while the other part of the data is processed by RAM.

On Linux, random access memory (RAM) is divided into sections called pages. Swapping is a process of copying memory pages to a specially allocated disk partition, called swap space, in order to free RAM. The combined amount of memory and swap partition is the amount of available virtual memory.

Swapping is necessary for the following reasons. Firstly, when the system needs more memory space (for example, if an app or process requests more memory from the system) than is currently available in RAM, the kernel unloads the least used pages and allocates freed memory to the current application or process. And secondly, a significant number of pages used by programs at the launch phase are only used during initialization and never again. The system can swap these pages freeing RAM this way.

How To Check Swap

You can get swap space information using free -h command:

[root@vps ~]# free -h
      total used free shared buff/cache available
Mem:   990M 406M  71M  49M     512M       390M
Swap:  1,0G  47M 976M

According to the result, the swap space is 1 GB.

⚠️Disabling the swap partition may affect the performance of your server. Disable swap only when you are sure it is necessary.

Deleting a swap partition

  1. Connect to the server via SSH (the SSH-access data can be found in the welcome email or requested from the support team), and disable swap with the following command:
    [root@vps ~]# swapoff -a
  2. Delete the line with the swap partition in the /etc/fstab file:
    [root@vps ~]# vi /etc/fstab

    In the vi editor window, move the cursor to the beginning of the line with the swap section and delete this line by double-clicking the d key (dd command). To save changes and close the editor, enter the following command: wq

  3. Go to the SolusVM portal (the access data can be found in the same welcome email). Go to the management section of the virtual server and click the Rescue button > Enable Rescue Mode. After enabling the Rescue mode, the current connection to the server via SSH will be interrupted and new data for connecting to the server in Rescue mode will appear. Сonnect to the server via SSH using this data.
  4. Delete and recreate the main and swap partitions:

    rescue # fdisk /dev/vda
    
    Welcome to fdisk (util-linux 2.29.2).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.

    Delete swap:

    Command (m for help): d
    Partition number (1,2, default 2): 2
    
    Partition 2 has been deleted.

    Delete the main partition:

    Command (m for help): d
    Selected partition 1
    
    Partition 1 has been deleted.

    Create a new partition:

    Command (m for help): n
    Partition type
    p primary (0 primary, 0 extended, 4 free)
    e extended (container for logical partitions)
    Select (default p): p
    
    Using default response p.
    Partition number (1-4, default 1): 1

    Click Enter:

    First sector (2048-41943039, default 2048):

    Click Enter:

    Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039):

    Check if the disk now has the right size:

    Created a new partition 1 of type 'Linux' and of size 20 GiB. Partition #1 contains a ext4 signature.

    Existing signatures should not be removed:

    Do you want to remove the signature? [Y]es/[N]o: N

    Just in case, add a bootable flag to the main partition:

    Command (m for help): a
    Selected partition 1

    Save the changes:

    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.

    Thus, we expand the main partition to the maximum.

  5. After that, execute the commands to expand the file system:
    rescue # e2fsck -f /dev/vda1
    e2fsck 1.43.4 (31-Jan-2017)
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    /dev/vda1: 34084/1240320 files (0.2% non-contiguous), 495831/4980176 blocks
    rescue # resize2fs /dev/vda1
    resize2fs 1.43.4 (31-Jan-2017)
    Resizing the filesystem on /dev/vda1 to 5242624 (4k) blocks.
    The filesystem on /dev/vda1 is now 5242624 (4k) blocks long.
  6. Return to the SolusVM portal and turn off the Rescue mode. After disabling the mode, the connection to the server will be interrupted again. Now connect via SSH with the usual data from the welcome email:
    [root@vps ~]# fdisk -l /dev//vda
    
    Disk /dev//vda: 21.5 GB, 21474836480 bytes, 41943040 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk label type: dos
    Disk identifier: 0x000958c2
    
    Device Boot Start End Blocks Id System
    /dev//vda1 * 2048 41943039 20970496 83 Linux

Done

Rate this article
Share this article
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
In this article