Swap space is used in Linux when there is insufficient physical memory (RAM) on your system to store the data currently being processed. When your system needs more memory, inactive memory pages are written to the disk, freeing up physical memory. Increasing swap space should not be considered as a solution to memory shortages. Swap space is located on disk drives, which have slower access times than physical memory. If your system is swapping often, you should add more physical memory, not more swap space.


Swap space in Linux is either a normal file in the file system, called a swap file, or a separate partition, or a combination of swap partitions and swap files. A dedicated swap partition is much faster, but it is easier to change the size of a swap file. If you know how much swap space you need, use a swap partition. If you are unsure, experiment with a swap file first, then make a swap partition when you know your requirements.


The swap partition is listed in the partition table, referenced in /etc/fstab, and viewable in the /proc/swaps file. There are also command-line utilities to display information about your swap space. To view the swap partition in the partition table, enter:

# fdisk -l | grep swap
Disk /dev/mapper/cl-swap: 2147 MB, 2147483648 bytes, 4194304 sectors


To view the swap partition (or file) in the /etc/fstab file, enter:

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


To display the contents of the /proc/swaps file, enter:

# cat /proc/swaps 
Filename				                Type		Size	  Used	   Priority
/dev/dm-1                               partition	2097148	  88296	   -1


The mkswap command is used to initialize a swap partition or a swap file. The syntax is:

# mkswap {device|file}


The swapon and swapoff utilities enable and disable devices and files for swapping, respectively. To display current swap information, use the “swapon –s” command. Output is identical to viewing the contents of /proc/swaps.

# mkswap {device|file}


Adding Swap Space


The swap partition or swap file must exist before it is initialized. Use fdisk or parted to create a swap partition. A swap file is created by using the dd command. Example:

# dd if=/dev/zero of=/swapfile bs=1024 count=1000000


To initialize a swap partition, type:

# mkswap /dev/xvdd1


To initialize a swap file, type:

# mkswap /swapfile


Initialized swap space is enabled by using the swapon command. To enable swapping on a swap file, enter:

# swapon /swapfile


To enable swapping on a swap partition, enter:

# swapon /dev/xvda3


Update the /etc/fstab file to enable the swap partition or swap file at boot:

# vi /etc/fstab
UUID=...     swap   swap   defaults   0   0
/swapfile    swap   swap   defaults   0   0


Viewing Swap Usage


View the /proc/meminfo file, or use other utilities such as free, top, and vmstat to view memory and swap space usage. Example:

# grep -i swap /proc/meminfo
SwapCached:         9472 kB
SwapTotal:       2097148 kB
SwapFree:        2008852 kB


To view swap usage by using the free command, enter:

# free | grep -i swap
Swap:       2097148       88296     2008852


Was this answer helpful? 0 Users Found This Useful (0 Votes)