Allocate swap after Ubuntu 14.04 LTS installation
First, to create 4,000 MB of swap space:
$ sudo dd if=/dev/zero of=/swapspace bs=1M count=4000
4000+0 records in
4000+0 records out
4194304000 bytes (4.2 GB) copied, 5.92647 s, 708 MB/s
or
$ sudo fallocate -l 4000M /swapspace
Next turn it into a usable swap file:
$ sudo mkswap /swapspace
Setting up swapspace version 1, size = 4095996 KiB
no label, UUID=7d1895e4-7ccf-42c6-979a-51ebddb49e91
Activate it:
$ sudo swapon /swapspace
Confirm active swap spaces:
$ cat /proc/swaps
Filename Type Size Used Priority
/swapspace file 4095996 0 -1
Next, add the following line to /etc/fstab to activate the new swap at boot:
/swapspace none swap defaults 0 0
See also this wiki page: https://help.ubuntu.com/community/SwapFaq
To answer your question indirectly, you don't need to manage swapfiles yourself. There is a package called swapspace which will dynamically add swap files as needed.
- sudo apt-get install swapspace
Then you are done. Your system will grow and shrink swap space as needed.
Follow these steps:
sudo dd if=/dev/zero of=/mnt/{filename}.swap bs=1M count={swap_size}
sudo mkswap /mnt/{filename}.swap
sudo swapon /mnt/{filename}.swap
sudo gedit /etc/fstab
- Add the following text at the end of the file,
/mnt/{filename}.swap none swap sw 0 0
Note: Replace {filename} with any name you want to set to the file and replace {swap_size} with the size you want to assign to the swap file. Be sure the size of the file must the twice larger than the memory size.