SWAP Memory

Swap memory is a space on a storage device (usually a hard drive or SSD) that is used as virtual memory when the physical RAM (Random Access Memory) is full. When the system runs out of RAM, inactive pages of memory are moved to the swap space to free up RAM for active processes.

This allows the system to continue running even if it exceeds the available physical memory, but it can be slower since accessing data from disk is much slower than accessing data from RAM.

In short, swap memory acts as an overflow area for RAM, helping to prevent crashes or slowdowns when the system runs low on memory. However, heavy reliance on swap can degrade system performance due to slower access speeds compared to RAM.


Configuring swap :

Step 1: Create a Swap File

  1. Determine the size of the swap file you want to create. For example, to create a 2 GB swap file, use the following command:

    sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
    • /swapfile is the location and name of the swap file (you can choose another path if desired).

    • bs=1M sets the block size to 1 MB.

    • count=2048 creates a 2 GB file (2048 MB). Change this value to suit the desired swap size (e.g., for 4 GB swap, use count=4096).

    This command creates a file filled with zeros at the location specified, which will be used as swap space.

Step 2: Set Correct Permissions for the Swap File

  1. Set the proper permissions on the swap file to ensure that only the root user can access it:

    sudo chmod 600 /swapfile

    This ensures that no one other than the root user can read or write to the swap file.

Step 3: Set Up the Swap Area

  1. Make the swap file usable by setting up the swap area:

    sudo mkswap /swapfile

    This initializes the file so that it can be used as swap space.

Step 4: Activate the Swap File

  1. Enable the swap file so it becomes active immediately:

    sudo swapon /swapfile

    You can verify that the swap is active with the following command:

    swapon --show

    This will display the swap file along with the used swap space.

Step 5: Make the Swap File Permanent (Persistent Across Reboots)

  1. To make sure the swap file is used automatically at boot time, you need to add it to the /etc/fstab file.

    • Open /etc/fstab in a text editor (e.g., nano):

      sudo nano /etc/fstab
    • Add the following line at the end of the file:

      /swapfile none swap sw 0 0
      • /swapfile is the path to the swap file you created.

      • none is the mount point, as swap files are not mounted as regular file systems.

      • swap specifies the file system type.

      • sw are the default options.

      • 0 means no need for dump backup.

      • 0 means no file system check (this is not applicable for swap).

    Save and close the file (CTRL+X, then press Y to confirm and Enter to save).

Step 6: Verify the Swap File Configuration

  1. Check if the swap file is active by running:

    free -h

    This will display your system's memory, including swap usage.

  2. Alternatively, you can verify the swap file using:

    swapon --show

    This command will list all the active swap spaces on your system.


Adjusting Swappiness:

Swappiness controls how aggressively Linux swaps memory to disk. The default value is 60, but you can lower it to make the system less likely to use swap.

  • To check the current swappiness value:

    cat /proc/sys/vm/swappiness
  • To temporarily change swappiness (e.g., to 10):

    sudo sysctl vm.swappiness=10
  • To make the change permanent, add it to /etc/sysctl.conf:

    sudo nano /etc/sysctl.conf

    Add the following line:

    vm.swappiness=10
    vm.overcommit_memory=1

Save and exit (CTRL+X, then Y and Enter).


Last updated