This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:
Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.
See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.
Linux RAM is composed of chunks of memory called pages. To free up pages of RAM, a “linux swap” can occur and a page of memory is copied from the RAM to preconfigured space on the hard disk. Linux swaps allow a system to harness more memory than was originally physically available.
However, swapping does have disadvantages. Because hard disks have a much slower memory than RAM, virtual private server performance may slow down considerably. Additionally, swap thrashing can begin to take place if the system gets swamped from too many files being swapped in and out.
Although swap is generally recommended for systems utilizing traditional spinning hard drives, using swap with SSDs can cause issues with hardware degradation over time. Due to this consideration, we do not recommend enabling swap on DigitalOcean or any other provider that utilizes SSD storage. Doing so can impact the reliability of the underlying hardware for you and your neighbors.
If you need to improve the performance of your server, we recommend upgrading your Droplet. This will lead to better results in general and will decrease the likelihood of contributing to hardware issues that can affect your service.
Before we proceed to set up a swap file, we need to check if any swap files have been enabled on the VPS by looking at the summary of swap usage.
sudo swapon -s
An empty list will confirm that you have no swap files enabled:
Filename Type Size Used Priority
After we know that we do not have a swap file enabled on the virtual server, we can check how much space we have on the server with the df
command. The swap file will take 256MB— since we are only using up about 8% of the /dev/sda, we can proceed.
df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda 20907056 1437188 18421292 8% / udev 121588 4 121584 1% /dev tmpfs 49752 208 49544 1% /run none 5120 0 5120 0% /run/lock none 124372 0 124372 0% /run/shm
Now it’s time to create the swap file itself using the dd command :
sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k
“of=/swapfile” designates the file’s name. In this case the name is swapfile.
Subsequently we are going to prepare the swap file by creating a linux swap area:
sudo mkswap /swapfile
The results display:
Setting up swapspace version 1, size = 262140 KiB no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb
Finish up by activating the swap file:
sudo swapon /swapfile
You will then be able to see the new swap file when you view the swap summary.
swapon -s Filename Type Size Used Priority /swapfile file 262140 0 -1
This file will last on the virtual private server until the machine reboots. You can ensure that the swap is permanent by adding it to the fstab file.
Open up the file:
sudo nano /etc/fstab
Paste in the following line:
/swapfile none swap sw 0 0
Swappiness in the file should be set to 10. Skipping this step may cause both poor performance, whereas setting it to 10 will cause swap to act as an emergency buffer, preventing out-of-memory crashes.
You can do this with the following commands:
echo 10 | sudo tee /proc/sys/vm/swappiness echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
To prevent the file from being world-readable, you should set up the correct permissions on the swap file:
sudo chown root:root /swapfile sudo chmod 0600 /swapfile
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Former Director of Community at DigitalOcean. Expert in cloud topics including LAMP Stack, CentOS, Ubuntu, MySQL, SSL certificates, and more.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
What is the > bs=1024 count=512k < in the above command?
How do we set the size? for example if I’m running on a 256mb plan
C
BS stands for blocksize and in this case it will be 1,024 bytes. Count is how many times to write this block, or 512,000 * 1,024 bytes = 512MB of SWAP
Please note that for security reasons it is advisable to enter something like:
source: http://www.cyberciti.biz/faq/linux-add-a-swap-file-howto/
For anyone who wants to increase their swapsize after they have followed this tutorial you can follow the below steps. With the increase in memory on the base packages from 256MB to 512MB, I wanted to create a swap file double the “physical” memory size ie 1GB instead of the 512MB made in this tutorial:
Minor suggestion is that you need sudo before those last two chown/chmod commands and I noticed the dd command took a few minutes so it would be reassuring to note this to avoid worry that something has gone wrong!
That’s correct you will want to either use sudo or do this as root.
And the dd command is writing directly to disk so it will take a bit of time depending on the size of the swapfile that is written.
Thanks for pointing that out.
Thanks, this worked great. You can see how much swap is being used by entering top -c right below the memory usage. Have yet to see my server use the swap - but its there if it needs it and its great that DigitalOcean uses a SSD so the swap should be quicker than other hosts.
Is there a downside to setting the swap to be very large? I have 2GB of memory and my site takes ~100mb… so would it be bad to have a 4GB swap?
Thanks for your article. My web site’s db (mysql) drop 3 - 4 times, i hope it will help
@danafrancey The only downside is that you use more diskspace for swap, and you probably don’t need to…
Optionally, to change the swappiness of the system, edit /etc/sysctl.conf And add a line at the bottom: vm.swappiness=20 Then type: sysctl -p This sets the system to swap memory only when RAM usage is more than 80%, default was swapping when RAM usage was more than 60%, which results in most of the services being swapped if you experience a traffic spike.
Tiny detail: The number 262140 found twice in the article should really be 524284 :-)
when can i see any memory been swapped?
30 mins after setting this and I type free
command:
total used free shared buffers cached
Mem: 508396 501272 7124 0 20472 340460 -/+ buffers/cache: 140340 368056 Swap: 524284 0 524284
You can use fallocate instead of dd to speed up the file creation - instead of manually writing each 0 like dd does, it just makes the container of the file:
is equivalent to
and takes under a second or two to execute.
Great tutorial! Luckily, I stumbled onto it by accident. For the benefit of other future newbies, a link to this tutorial should be referenced in other rudimentary HowTos… just a suggestion.
silly but can u have more than 1 swap file? i mean… can i have like… 5 swap files using 30% of my host space? buddy press is memory hungry :(
Disadvantages section : "Because hard disks have a much slower memory than RAM, virtual private server performance may slow down considerably. "
But DigitalOcean uses a SSD , not hard drive. So this statement is useless.
What exactly does a droplet use swap for, and how much is optimal?
Thanks
@leo: You shouldn’t have more than 1 swap file - you should instead increase the size of your swap file.
@yangyun: It’s not useless - even SSDs are still slower than RAM.
As per the article:
Linux RAM is composed of chunks of memory called pages. To free up pages of RAM, a “linux swap” can occur and a page of memory is copied from the RAM to preconfigured space on the hard disk. Linux swaps allow a system to harness more memory than was originally physically available.
In case of debian you might have to run the command ‘swapon’ with ‘sudo’, otherwise it might return “command not found”.
Or use ‘/sbin/swapon -s’ mostly because /sbin is not added to the PATH of a normal user by default.
Super easy and worked perfectly. Capistrano wasn’t deploying because it said I didn’t have enough RAM to precompile my assets ["Rake aborted! Cannot allocate memory - nodejs … ]. Enabled 1GB swap file. Voila, problem solved.
Thanks!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.