How to Increase the Swap File in UNIX(-like) Operating Systems
First of all, I’m not saying on which operating system because this applies to numerous UNIX, UNIX derivates and UNIX-like operating systems. For example, the process of increasing the swap file is the same on all Linux, AIX, HP-UX, FreeBSD, NetBSD, OpenBSD, IRIX, Tru64 and possibly more. So, assuming that we have approximately 2GB of swap…
root:~# free total used free shared buffers cached Mem: 1026140 690164 335976 0 226408 246340 -/+ buffers/cache: 217416 808724 Swap: 2064376 0 2064376 root:~#
We first create an empty file with the size of additional swap we need. For example if we need 512MB of additional swap we’ll create a file like this:
root:~# dd if=/dev/zero of=/example_swap bs=1024 count=500000 500000+0 records in 500000+0 records out 512000000 bytes (512 MB) copied, 7.03446 seconds, 72.8 MB/s root:~#
Which will obviously create file with size of 512MB filled with zeros…
root:~# ls -l /example_swap -rw-r--r-- 1 root root 512000000 May 24 11:49 /example_swap root:~#
Next, we are making this a swap area using the provided utility.
root:~# mkswap /example_swap Setting up swapspace version 1, size = 511995 kB root:~#
And we attach this to the system’s swap file as shown below…
root:~# swapon /example_swap root:~# free total used free shared buffers cached Mem: 1026140 1005904 20236 0 197632 579308 -/+ buffers/cache: 228964 797176 Swap: 2564368 0 2564368
As you can see the space was increased. It would be wise to also update /etc/fstab to map this area at boot time or when using swapon -a command.
In order to stop using this area, simply remove it with the appropriate system call’s wrapper utility.
root:~# swapoff /example_swap root:~# free total used free shared buffers cached Mem: 1026140 1005780 20360 0 197648 579316 -/+ buffers/cache: 228816 797324 Swap: 2064376 0 2064376 root:~#