What is Swap Memory in Linux? - TechArticles

What is Swap Memory in Linux?

1. What is swap in Linux?

Swap space in Linux is used when the amount of the Physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in the memory are moved from RAM to swap space. It helps the machines which are having small amount RAM and it should not be considered a replacement for more RAM. Swap is located on the hard disks which have slower access time than Physical memory.
Swap Memory in Linux
{getToc} $title={Table of Contents}

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 size is less than or equal to 2 GB, then the size of the swap = 2 X RAM size.

* If the RAM size is more than 2 GB, then the size of the swap = 2 GB + 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:

If we run or open any application, it requires some amount of memory to load its features. So, first it looks or occupy physical memory (RAM). If there is not enough space in RAM, the application's data is transferred from RAM to swap space. If the pages are moving from RAM to swap space, that is called swap-in or page-out.

swap-out or page-in:

If older or previous application is closed, then the space occupied by those applications also cleared. ie., some of the space is available in RAM. So, automatically some data which is already occupied in swap space is also moved from swap to RAM. If the pages are moving from swap space to RAM, that is called swap-out or page- in.

5. How paging space is allocated?

(i) Paging means data transferred from RAM to swap space.

(ii) If we open or run any application, first it will occupy the required space in RAM. If there is not enough space in RAM, then some amount of application's data will be transferred from RAM to swap space. ie., swap space is allocated to that application. This allocation is called paging space or page-out allocation.

(iii) paging will takes place in swap by blocks. First it will create the required no. of blocks in swap space.

(iv) If RAM space cleared by older or other applications, then swap occupied data is transferred from swap to RAM. This is called page-in. So, that much amount of space is unallocated in swap ie., removed the created blocks in 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?

(i) By creating a new swap partition on the disk. (separate swap partition)

(ii) 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?

(i) Users cannot login to the system.

(ii) If already login users not able to execute any command.

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

(i) The new applications cannot load due to lack of memory.

(ii) So, users cannot login to the application and cannot access the applications features. 

(iii) 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 ?

(i) We can restore the data from backup by, tar, cpio, dd, net backup or other tools.

(ii) If it is in mirror, we can sync the data from mirrored disk.

(iii) 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

Jay

I love keeping up with the latest tech trends and emerging technologies like Linux, Azure, AWS, GCP, and other cutting-edge systems. With experience working with various technology tools and platforms, I enjoy sharing my knowledge through writing. I have a talent for simplifying complex technical concepts to make my articles accessible to all readers. Always looking for fresh ideas, I enjoy the challenge of presenting technical information in engaging ways. My ultimate aim is to help readers stay informed and empowered on their tech journeys.

Post a Comment

Previous Post Next Post

Contact Form