The ultimate Linux interview questions : swap

What is swap space?

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the physical memory is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

What’s a swap partition?

Swap partitions are very different from filesystems. Filesystems (usually) store trees of directory nodes to reference file inodes, which then reference the blocks in files. Everything on disk has or had a name once, and you can reconstruct which blocks are in which files or directories or are no longer in use pretty easily. (fsck does this on every unclean reboot.)

Swap partitions contain “random” pages of memory from processes that weren’t used often enough to keep them out of swap. Those chunks won’t include libraries or the executables. Swap is only going to contain pages created on the fly by the applications for data. There is no ordering information about the swap actually held in the swap partition, all of that information is meaningful only when the machine is running, and the machine is likely to need it in a hurry, so the page mapping tables are stored in unswappable kernel memory.

What’s a swap file?

In some situations it’s difficult to add a new swap partition. Linux doesn’t allow to change partition definitions and if we cannot add new partitions without dropping existing partitions. In this case swapfiles may be helpful.

How to add a swap file?

Determine the size of the new swap file and multiple by 1024 to determine the block size. For example, the block size of a 64 MB swap file is 65536. At a shell prompt as root, type the following command with count being equal to the desired block size:

# mkdir /data2
# dd if=/dev/zero of=/data2/swapfile1 bs=1024 count=65536

Setup the swap file with the command:

# mkswap /data2/swapfile1

To enable the swap file immediately but not automatically at boot time:

# swapon /data2/swapfile1

To enable it at boot time, edit /etc/fstab to include:

/data2/swapfile1 swap swap defaults 0 0

The next time the system boots, it will enable the new swap file.

How to add swap partition and swap volume ?

Refer the post below to get detailed procedure.

How do I find out how much swap space is configured on my linux?

Use the swapon -s command to get the swap details. This will tell you how much has been allocated for swap and how much is being used. For example:

# swapon -s
Filename           Type     Size    Used    Priority
/dev/dm-0                   partition   2097148 0       -1

What the difference between swapping and paging?

Swapping is one of the Unix mechanisms to accommodate the size limitation of memory by moving entire processes to disk to reclaim memory.

Paging is another Unix mechanism to manage the limitations of memory. Unlike swapping, where entire processes are moved in and out of memory, paging moves only individual pages of processes to disk. Paging is not as serious a problem as swapping, as the entire program does not have to reside in memory to run. A small amount of paging may not noticeably affect the performance of a system. However, the performance of a system may degrade rapidly as paging activity increases.

Swap space on disk is used to hold pages of memory that have been paged or swapped out. A shortage of swap space may cause symptoms such as system hanging, poor response times, and unsuccessful spawning of new processes.

How can I enable swap ?

First check if swap is enabled:

# swapon -s

To enable swap, check for swap entries in your /etc/fstab

# grep swap /etc/fstab
/dev/mapper/centos-swap swap                    swap    defaults        0 0

and use the ‘/sbin/swapon -a’ command to enable all Swap partitions listed in /etc/fstab.

# swapon -a

How to remove swap partition?

If the swap partiton is not currently in use you can umount the swap with the swapoff command. At a shell prompt as root, execute the following command to make sure the swap partition is disabled (where /dev/hdb2 is the swap partition):

# swapoff /dev/hdb2

Remove its entry from /etc/fstab. Remove the partition using parted or fdisk.

How to remove swap file?

As root, execute the following command to disable the swap file (where /swapfile1 is the swap file):

# swapoff /data2/swapfile1

Remove its entry from /etc/fstab and remove the actual file:

# rm /data2/swapfile1

How to remove swap partition?

If the swap partiton is not currently in use you can umount the swap with the swapoff command. At a shell prompt as root, execute the following command to make sure the swap partition is disabled (where /dev/hdb2 is the swap partition):

# swapoff /dev/hdb2

Remove its entry from /etc/fstab. Remove the partition using parted or fdisk.

How to remove swap file?

As root, execute the following command to disable the swap file (where /swapfile1 is the swap file):

# swapoff /data2/swapfile1

Remove its entry from /etc/fstab and remove the actual file:

# rm /data2/swapfile1

How to check swap usage

To check swap usage, you can use any one of the utilities below:
1. free

# free
             total       used       free     shared    buffers     cached
Mem:       1004608     934036      70572          0      30124     769640
-/+ buffers/cache:     134272     870336
Swap:      1060248          0    1060248

2. /proc/meminfo

# grep Swap /proc/meminfo
SwapCached:            0 kB
SwapTotal:       1060248 kB
SwapFree:        1060248 kB

3. top

# top
.......
Mem:   1004608k total,   934780k used,    69828k free,    30124k buffers
Swap:  1060248k total,        0k used,  1060248k free,   770156k cached
.......

4. vmstat

# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0  69828  30124 770156    0    0    40   403   23   22  0  1 99  0  0
Related Post