What is Swap Memory in Linux? – TechArticles

by Jay

1. What is swap in Linux?

In Linux, a “swap” refers to a special type of space on the hard disk that is used as virtual memory. When the physical RAM (Random Access Memory) becomes insufficient to handle the running applications and their data, the Linux operating system uses this swap space as an extension of RAM.
Swap Memory in Linux

2. What is the recommended swap space?

Generally the recommended swap space is double the RAM size, but the following table shows actual amount. Apart from the below recommendation a basic rule is applied to create the swap partition.

  • If the RAM capacity is 2 GB or less, the swap size is determined as 2 times the RAM size.
  • However, if the RAM capacity exceeds 2 GB, the swap size is calculated as the sum of 2 GB and the RAM size.
Amount of RAM in the System Recommended Amount of Swap Space
4 GB or less Minimum 2 GB
4 GB – 16 GB Minimum 4 GB
16 GB – 64 GB Minimum 8 GB
64 GB – 256 GB Minimum 16 GB
256 GB – 512 GB Minimum 32 GB

3. Is it necessary to create the swap at the time of installation?

Yes, swap space is compulsory to be created at the time of installation. But additional swap space can be created and deleted at any point of time, when it is required. Sometimes we need to increase the swap space, so we create additional swap space which will be added to the existing swap space to increase the size.

4. What is swap-in and swap-out or page-in and page-out?

  • Swap-in or page-out:
  • When launching or accessing an application, a certain amount of memory is necessary to initiate its features. Initially, the application seeks and reserves a portion of physical memory, also known as RAM. If there isn’t sufficient space available in the RAM, the application’s data is then shifted from the RAM to an area called swap space. This transition of pages from RAM to swap space is referred to as swap-in or page-out.

  • Swap-out or page-in:
  • When an older or previously used application is shut down, the memory space it was using is freed up. This action makes a portion of the RAM available once again. As a result, certain data that was previously stored in the swap space is automatically transferred from the swap space back into the RAM. This process, in which pages are relocated from the swap space to the RAM, is referred to as swap-out or page-in.

    5. How paging space is allocated?

    • Paging involves the movement of data from the RAM to the swap space.
    • Upon launching or executing an application, the initial step is to reserve the necessary memory within the RAM. In instances where there's insufficient RAM capacity, a portion of the application's data is relocated from the RAM to the swap space. This assignment of space in the swap area to the application is known as paging space or page-out allocation.
    • Paging occurs in the swap space through the use of blocks. Initially, the required number of blocks is generated within the swap space.
    • When the RAM is freed up due to the closure of older or other applications, data that was occupying space in the swap is transferred back from the swap space to the RAM. This process is termed as page-in. Consequently, the corresponding portion of space becomes unallocated within the swap space, leading to the removal of the previously created blocks in the swap.

    6. How to create the swap partition in Linux with command?

    Check the available disks in the system

    # fdisk -l 
        - It will list all the available disks lets suppose we get disk /dev/sdb

    Below command will help to create partition

    # fdisk /dev/sdb 
       Command (m for help) : n (to create a new partition) 
       First cylinder :
       (press Enter key) 
       Last cylinder : +2048M
       Command (m or help) : t (to change the hex code)
       Partition no. (1-2) : 2 (to change the partition number hex code) 
       Hex code : 82 (82 is the hex code for Linux swap) 
       Command (m for help) : w (write the changes to the disk)

    Update the partition table information

    # partprobe or 
    # partprobe /dev/sdb

    Use below command to convert the raw disk to swap file system

    # mkswap /dev/sdb2
    # swapon /dev/sdb2        ----to turn on the swap partition 

    Make the permanent mount of swap partition

    # vim /etc/fstab
       /dev/sdb2 swap swap defaults 0 0 
       (save and exit this file with :wq) 

    Mount all the partitions which are having entries in /etc/fstab file and check the total RAM and swap size by running free -m

    # mount -a
    # free -m 

    7. How to remove the swap partition in Linux?

    Check swap partition names or disks by running command

    # swapon -s

    Turn off the swap partition and remove the fstab entry

    # swapoff /dev/sdb2 
    # vim /etc/fstab 
     - open this file and remove the swap partition previously added entry and after removing the swap partition and exit by saving the file.

    Run the below command to delete the complete swap partition from the disk

    # fdisk /dev/sdb  
    Command (m for help) : d    ---(d for to delete the partition) 
    Partition no. (1-2)  : 2
    Command (m for help) : w (to write the changes into the disk)
    # partprobe or 
    # partprobe /dev/sdb
    # free -m       (to see the RAM as well as swap sizes) 

    8. In how many ways can we create the swap spaces in Linux?

    1. By creating a new swap partition on the disk. (separate swap partition)
    2. By creating swap file.

    9. How to create the swap space using the swap file in Linux?

    Sometimes it is unable to create a swap partition because may be there is no disk space or may be the partition limit is already exceeded. So, in these scenarios we have to create only the space file.

    Use below command to create swap partiotion. In this tutorial creating 2048 MB of file for swap.

    # dd if = /dev/zero of = /root/linuxswap bs = 1M count = 2048
    # du - /root/linuxswap (to see the linuxswap size)
    # mkswap /root/linuxswap (to convert the existing file system to swap file system)
    # swapon /root/linuxswap (to turn on the swap file)
    # vim /etc/fstab (to make a permanent mount of swap space)
       /root/linuxswap swap swap defaults 0 0 
            - Add above entry in fstab then save exit this file by :wq
    # mount -a (to mount all the partitions which are having entries in /etc/fstab file)
    # df -hT (will not show the swap size) 
    # free -m (to see the total RAM and swap size)

    10. What is virtual memory?

    • The combination of Physical memory (RAM) and swap space is called the virtual memory.
    • So, Virtual memory = Physical memory (RAM) + swap space.

    Other useful commands :

    # swap -s   (to see how many swap partitions are there and with their names)
    # swapon -a      (to turn on all the swap partitions)
    # swapoff -a     (to turn off all the swap partitions)
    # cat /etc/mtab  (to see the current and temporary mount points)
    # mountpoint <directory or mount point>    (to check the specified directory is a normal directory or a mount point)
    # df -ih (to check how many inode numbers are available in the mounted partitions)

    11. What happens when the /usr is full?

    • Users cannot login to the system.
    • If already login users not able to execute any command.

    12. What happens when memory ie., pagein space is full?

    • The new applications cannot load due to lack of memory.
    • Consequently, users are unable to log in to the application and access its features.
    • So, if we increase the swap memory to the required size then the problem will be solved.

    13. How to restore the data and upgrade your O/S ?

    • We can restore the data from backup by, tar, cpio, dd, net backup or other tools.
    • If it is in mirror, we can sync the data from mirrored disk.
    • We can upgrade the O/S in two ways.

    (a) Online :

    The O/S is upgraded from previous to present while the system is running. It is risky and takes long time.

    (b) Offline :

    First take backup of all the system and then remove previous O/S and install the present O/S and restore the backup from backup disks or tapes. So, it is very easy and non-risky job

    Related Posts

    Leave a Comment

    * By using this form you agree with the storage and handling of your data by this website.