Here is a basic how-to guide on setting up and managing a swap file. I am using Ubuntu 12.04 but I am sure it applies to other versions and Linux distros. I recently had to figure out how to increase the size of an existing swap file so I thought I would throw together a guide(see section below "Create Swap file` if you need to do this).
A swap file(called a page file in Windows) is a way to increase memory/RAM capacity without upgrading hardware (virtual memory). It is simply a file on your hard drive. When your physical sticks of RAM get full, the system will copy over to the swap file to avoid a crash or lose data. Swap files will be slower at reading and writing because they reside on your hard drive but is better than a complete failure.
See table of useful commands (cheat sheet) when dealing with swap files at the bottom of this article.
You need to create a big file the size of your swap. Use dd
or fallocate
.
ex:
Create a 512MB or .5GB swap file: sudo dd if=/dev/zero of=/swapfile bs=1024 count=524288
Create a 1024MB or 1GB swap file: sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
bs*count
in bytesif=/dev/zero
makes it read from that file. /dev/zero
returns as many null characters that are read.of=/swapfile
determines the namebs
is the number of bytes in a chunkcount
is the number of chunksRun: sudo mkswap /swapfile
Run: sudo swapon /swapfile
fstab
/etc/fstab
. You could use: sudo nano /etc/fstab
/swapfile none swap sw 0 0
If you want to learn more about the fstab
syntax/configuration, see this fstab Ubuntu doc
0600
:This will only allow the owner(root) to read and write to the file.
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
Swappiness is a value 0 to 100. A value of 0 means that the swap file will be avoided until actually necessary (run out of memory). A value of 100 will put stuff into the swap file as soon as possible. The default is 60. You can think of the number as the percentage left of actual RAM when the system starts copying into the swap file.
A swappiness value of 10 will make it act as an emergency airbag to avoid out of memory
errors.
Run both commands:
echo 10 | sudo tee /proc/sys/vm/swappiness
OR sudo sysctl vm.swappiness=10
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
The first one modifies the swapiness variable in the virtual memory(vm) process(not permanent). And the second makes it permanent by adding it to the conf which gets loaded in at boot time.
You can see how tee
works here. It just overwrites the file and displays what is written. The -a
parameter appends the text instead.
Alternatively you can just:
/etc/sysctl.conf
. You could use sudo nano /etc/sysctl.conf
vm.swappiness
. If it doesn't exist, add it to bottom of the file: vm.swappiness=10
Ctrl+x
to exit, then press y
to save.You can check your swappiness value using this command: cat /proc/sys/vm/swappiness
Run sudo swapon -s
to see a summary of your swap file setup. It should be listed.
Also if you run free -m
, it should show the split in usage between mem and swap.
Just create a new swapfile (follow instructions above), then deactivate the old one. Don't forget to update the fstab
.
To deactivate a swapfile: sudo swapoff /swapfile
To delete the swapfile: sudo rm /swapfile
Command | Description |
---|---|
sudo swapon -s | List swap files |
free -m | Show memory usage breakdown |
cat /proc/sys/vm/swappiness | Check swappiness value |
sudo swapon -a | Turn on all swap files |
sudo swapon /swapfile | Turn on swap file specified |
sudo swapoff -a | Turn off all swap files |
sudo swapoff /swapfile | Turn off swap file specified |