How To Create A Swap File in CentOS

SumodirjoDIY, Open Source, Tutorial

In this tutorial we’ll be learning how to create a swap file in CentOS. When we install Linux, part of the process when creating a partition is creating a swap partition. A Swap is more less like Windows file. When your RAM is full Linux try to move some data from RAM to the swap file, but it doesn’t replace your RAM since your swap partition or swap file is located on hard drive which is a lot slower than your RAM. Another usage of a swap file is when you want to hibernate your laptop or desktop.

When RAM size was still small the rule of thumb to create a swap file is twice your RAM size. For example if you have 256MB of RAM the you have to create 512MB of swap, but since now you might have 4GB or even 16GB on your desktop, creating 32GB swap partition which would be not used most of the time is waste of space. You can read Red Hat recommendation about the size of swap.

When you install linux it will ask you to create a swap partition but if you need additional swap space on the fly you can create a swap file. More or less it’s like a swap partition but instead of having a partition you have a file.

For example, we will create a new file blank file with dd

[root@clarisa sumodirjo]# dd if=/dev/zero of=/swapswap bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 1.83954 seconds, 57.0 MB/s

the command above will create swapswap file on / (root) directory, with the size 100MB (1M x 100). The file will be filled with zere (/dev/zero)

Now, we have file for swap, it’s time to create swap with mkswap on the “swapswap” file that we created before:

[root@clarisa sumodirjo]# mkswap /swapswap
Setting up swapspace version 1, size = 104853 kB

The swap file is ready but not yet activated. To see current swap size, we can use free -m, the -m option will make memory output printed in MB

[root@clarisa sumodirjo]# free -m
             total       used       free     shared    buffers     cached
Mem:           376        269        107          0          2        145
-/+ buffers/cache:        121        254
Swap:          767          0        767

The output above said that we have swap 767MB. To activate our new swap file, we can use swapon command:

[root@clarisa sumodirjo]# swapon /swapswap

Let’s see again our swap size with free -m :

[root@clarisa sumodirjo]# free -m
             total       used       free     shared    buffers     cached
Mem:           376        269        106          0          2        145
-/+ buffers/cache:        121        254
Swap:          867          0        867

And we get additional 100MB of swap from our swap file.

Now, how to disable swap file? it’s easy, you can use swapoff command, for example:

[root@clarisa sumodirjo]# swapoff /swapswap

And when we check with free -m

[root@clarisa sumodirjo]# free -m
             total       used       free     shared    buffers     cached
Mem:           376        271        105          0          2        147
Swap:          767          0        767

If you want to make this swap file loaded on boot, you can add the swap file to /etc/fstab, for example :

/swapswap   swap    swap    defaults 0 0