How to Use a Swap File for Your System

If you have very little resource to work with a very wide scope of processes, you might want to have a swap space with your machine. Actually having real RAM is always the best solution but what GNU/Linux acutally does with SWAP is putting the inactive and init processes which are only used in initialization other processes and never used later, over swap. So that won’t make the performance worse but keeping the system attain in some points of working.

To see if your system have working swap system.

1
$ swapon -s

You will see the working swap systems listed if they are already on your system. Or we are going to make one.

Create a swap file which is 512 MB in size. ( 1024 * 512 = 524288 block size )

1
$ dd if=/dev/zero of=/swap0001 bs=1024 count=524288

[ /dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it.[1] One of the typical uses is to provide a character stream for initializing data storage. wiki]

And secure the swap file.

1
2
$ chown root:root /swap0001
$ chmod 0600 /swapfile1

Let the system know.

1
2
$ mkswap /swap0001
$ swapon /swap0001

To activate the created swap partion on the reboot, edit /etc/fstab:

[ The fstab (or file systems table) file is a system configuration file commonly found at /etc/fstab on Unix and Unix-like computer systems. In Linux it is part of the util-linux package. The fstab file typically lists all available disk partitions and other types of file systems and data sources that are not necessarily disk-based, and indicates how they are to be initialized or otherwise integrated into the larger file system structure. wiki]

1
2
3
$ swapon -s 
Filename              Type        Size    Used    Priority
/swapfile1                              file      524284  0   -1

Copy the second output line made from “swapon -s” command and paste it onto /etc/fstab

1
$ vi /etc/fstab

/swapfile1 file 524284 0 -1

Then check the system.

1
2
3
4
5
$ free -m
             total       used       free     shared    buffers     cached
Mem:          1003        930         72          0         71        652
-/+ buffers/cache:        206        796
Swap:          511          0        511

Have Fun

Copyright © 2016 - Tin Aung Linn. Powered by Octopress