Computers only have a finite amount of RAM. When it runs out, they will "swap out" parts of memory that is not crucial to disk, soit can be read back in when necessary. As disks are much slower than RAM, even with modern SSD drives, having more RAM is always preferable to swapping, but it's still a good idea to have swap space available for when it is required. If you don't, you might find the "oom killer" has killed an important process to make some memory available to others. You really don't want your database getting killed just because PHP requires some extra memory because it's resizing a large image.
If it is things like this where your system load is in need of a bit of extra memory only for a while and you can't just add in some extra RAM, it is good to have swap space available for those peak memory loads. Windows handles this automatically to the point where most users won't be aware of it. Linux will leave the configuration up to the user, and it is generally handled by setting up a swap partition when you install the distribution. If you (or the person who configured your Linux system) haven't done so, or your swap partition is of insufficient size, you might want to look into creating a swapfile. Here's how to do that.
How much do you need?
First, determine how much swap space you need in total. A good rule of thumb is to look at how much physical RAM you have (the Ubuntu SwapFaq gives some rule of thumb). For the maximum amount of swap space, you generally shouldn't exceed twice the amount of physical RAM you have. For the minimum, if your RAM is 1GB or less, make the swap file at least that same size. For more, take the square root of your RAM size in GB and round it to the nearest gigabyte. Here's a table for common RAM sizes:
|RAM||Minimum swap||Maximum swap|
You can check how much swap space has been set up with the swapon command without any further parameters. If this gives no output, that means no swap space was configured.
Making the swap file
Note: all the commands here should be executed as root, either through a root shell, or through sudo.
First, use dd to create the actual file you want to use. For my example, I'm going to be using /swapfile at a size of 2GB. As 2GB (technically 2GiB, but I hate the way that sounds) is 2048MB (again, technically MiB), make dd read zeroes from /dev/zero for 2048 blocks of 1MB:
dd if=/dev/zero of=/swapfile bs=1M count=2K
The breakdown of this command is as follows:
- dd is a command to copy data efficiently from one device to another
- set the input file (source) to be /dev/zero which supplies an endless stream of zeroes
- the output file (destination) is /swapfile, the file we're creating
- use a block size of 1M bytes (1048675 bytes, considering this is binary)
- repeat that block size 2048 times, making for a total of 2 gigabytes
This will take a few seconds, after which the file will have been created. Then, you should make sure that it has the correct permissions, where only the root user can access it:
chmod 600 /swapfile
Now that that is done, we can prepare the file to actually be used as swap space:
And finally, it can be enabled:
That's really all there is to it, but keep reading.
Enabling swap on boot
Whenever you reboot your system, the newly created swap file will not be used automatically, unless you configure the system to do so. For this, it should be added to the filesystem database in /etc/fstab. Use your favourite text editor (again, as root) to append a line to that file:
/swapfile none swap sw 0 0
The values have the following meanings:
- The first column is the device name - in this case, the filename of the swapfile.
- The second column is the mount point - it's not being mounted, so none is appropriate.
- The third column is the filesystem type, which is swap.
- The fourth column has the mounting options - for a swap partition, this should be sw. For a swapfile, it might also be defaults, but I prefer sw for consistency.
- The fifth column is for use by the dump backup program, but backing up a swapfile is useless, so set it to 0.
- The sixth and final column specifies the order in which to check the filesystem for errors. As the swapfile resides on another filesystem, it need not be checked for errors itself and the contents of the swapfile or partition are useless on system boot anyway so set that to 0 too.
Need more or less?
If the swap size is insufficient, you can simply add another swapfile by repeating the steps above. Just be sure to give it a different name. You can also remove a swapfile by removing the entry in /etc/fstab, disabling the swapfile with the swapoff /swapfile command, and then the actual file can be removed.