Here is a basic howto 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 hardware 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 (cheatsheet) when dealing with swap files at the bottom of this article.
You need to create a big file the size of your swap. Use
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
if=/dev/zeromakes it read from that file.
/dev/zeroreturns as many null characters that are read.
of=/swapfiledetermines the name
bsis the number of bytes in a chunk
countis the number of chunks
sudo mkswap /swapfile
sudo swapon /swapfile
/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
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/swappinessOR
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:
Ctrl+xto exit, then press
You can check your swappiness value using this command:
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
To deactivate a swapfile:
sudo swapoff /swapfile
To delete the swapfile:
sudo rm /swapfile
||List swap files|
||Show memory usage breakdown|
||Check swappiness value|
||Turn on all swap files|
||Turn on swap file specified|
||Turn off all swap files|
||Turn off swap file specified|