Site Search

2008-11-22

Tuning swap: Create a swap file to reduce maintenance effort and complexity

In recent kernel versions the performance overhead of swap files compared to swap partitions become insignificant. So you should consider using swap files instead of dedicated partitions to reduce the maintenance effort and complexity of your system. Actually using swap files does not decrease the data throughput and performance of your swap setup. So you can consolidate swap partitions by outsourcing them to dedicated swap files.

First, you have to create a file of a predefined size with a predefined block size.
dd if=/dev/zero of=/swap bs=1024 count=2097152
The statement above, creates a file named "swap" that is located in your root partition ("/") with the block size of 1024 KB which has the overall size of 2 GB (2097152 KB = 1024 KB * 2048).

Now you have to give the file the swap format using the command bellow.
mkswap /swap


You can test your new swap by activating it:
swapon /swap


Hereby it is not necessary to reboot the machine and you can see whether the new swap space is available by executing the
free -m
command.

Now, you have to tell your system to use the new swap space by adding the following line to the /etc/fstab:
/swap swap swap defaults 0 0
Note:
You also may be interested in how to tune ext3 for performance or how to tune ext4 for performance.