Swap is space on a disk that is reserved to be used as virtual memory. When a Linux server runs out of memory, the kernel can move inactive processes into swap to make room for active processes in the working memory.
1. Create 'swapfile' file with
1. Create 'swapfile' file with
dd command in / (root's home dir that is '/'). 'dd' Linux commands primary purpose is to convert and copy files.dd if=/dev/zero of=/swapfile bs=1M count=4048dd = It is a unix command used for convert and copy a fileif = read from FILE instead of stdin/dev/zero = /dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from itof = write to FILE instead of stdout/swapfile = file named swapfile will be created in /bs = Read and write bytes at a time but if you do not mention MB or GB like only number it will read as bytes. for eg. bs=1024 means 1024 bytescount = Copy input blocks in our case it is 1024 (1M * 4048 = 4GB)2. Run mkswap command to make swap area:
mkswap /swapfile3. Change the permission of file swapfile-additional:
chmod 600 /swapfile4. Permanent mounting the swap space by editing the
Use your file editor, I generally use vi editor:
/etc/fstab file:Use your file editor, I generally use vi editor:
vi /etc/fstab Add the content in
/etc/fstab file: /swapfile swap swap 0 05. Now mount the swap area:
mount -a6. Enable the swap area:
swapon -a7. Check the number swap space mounted on your system:
swapon -s8. To check how much is swap space available on system.Run below given command:
free -mYou can try creating swapfile with different sizes on your local machine or test VM.
To remove a swap file:
1. At a shell prompt as root, execute the following command to disable the swap file (where /swapfile is the swap file):
swapoff -v /swapfile2. Remove its entry from the
/etc/fstab file.3. Remove the actual file:
rm /swapfile