• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • Forums
  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings
Home » AWS, Azure, Cloud » Swap File For Ubuntu On Amazon EC2 – Why And How?

Swap File For Ubuntu On Amazon EC2 – Why And How?

By Ehi Kioya Leave a Comment

On Amazon EC2, the instance type you choose determines the amount of physical memory (RAM) that you get. Larger instance types with lots of RAM are more expensive. So if you want more RAM on a medium or small EC2 instance, you can define some storage space on your disk to act as RAM when needed. The disk area you use for this purpose is called a swap file. This article explains swap files and demonstrates how to set up a 2GB swap file using ephemeral storage on an m3.medium instance of Amazon EC2 that has only 3.75GB of RAM by default.

The how to section of this article applies to Ubuntu EBS-backed Amazon EC2 instances but it should also work for any debian-based distro as well. If your set up is significantly different from mine (not EBS backed for example), you can still use this article as a guide but you’d have to figure out the details yourself.

UPDATE: While this article focuses on adding a swap file to Amazon EC2 instances that come with instance/ephemeral storage, I have written another article that discusses how to add a swap file to Amazon EC2 instances that are “EBS Only” (without instance/ephemeral storage). Read it here: Adding A Swap File To An “EBS Only” Amazon EC2 Instance

Swap Spaces And Swap Files

Swap space gets used when your system needs physical memory for active processes and there is not enough unused physical memory available. It is disk space used in Linux (and other operating systems) to store memory objects when actual RAM starts becoming crowded. Since disk is slower than memory, Linux first stores in swap the least recently used objects, and keeps as much as of it as possible in memory. If possible, the general recommendation is to have a swap space equal in size to your memory.

Since the access time for swap is slower, it is not meant to be a complete replacement for the actual RAM. Swap space can be a swap file or a dedicated swap partition, or a combination of both. This article discusses swap files.

Why You Need A Swap File On Amazon EC2

Amazon EC2 instances never seem to come pre-configured with swap files (at least the ones I’ve played with didn’t). You can easily check if your instance has a swap file by using any of these three methods:

  • free command
  • top command (there’s a line for swap before the list of processes)
  • swapon -s command

For example, here’s a screen shot of the top command output with the swap file line highlighted in yellow.

Swap File On Amazon EC2

An m3.medium instance of EC2 comes with 3.75GB of physical memory. While this might be sufficient for a lot of stuff, if your web application is reasonably sized, that memory could get used up pretty fast and you might start seeing errors in processes like MySQL and others. From my experience many of such memory related errors can be fixed by the addition of a swap file.

Where Should The Swap File Be Set Up?

EBS backed EC2 instances have their root volume on EBS (Elastic Block Store) and they also have another type of storage called “instance storage” or “ephemeral storage” that is physically attached to the machine. Data stored on ephemeral storage is not persistent (it disappears when the instance is stopped but not when it is rebooted). Now you have two options for where to create your swap file: Either directly on EBS or on ephemeral storage.

A major deciding factor is that EBS is not free (you pay for I/O operations) but ephemeral storage is always free. And since RAM is not persistent as well, ephemeral storage has sort of become the default place for swap files. It’s as if swap files and ephemeral storage have been made for each other.

Ephemeral means “lasting for a very short time”.

There are some situations, however, where you would want to (or have to) set up your swap file on an EBS volume. For example, t1.micro instances do not support ephemeral storage so you really have no choice but to use EBS in that situation. Here, I’ll be setting up a swap file on ephemeral storage but you can easily adapt the instructions to work for EBS.

Ephemeral storage is mounted at /mnt after startup.

How To Set Up The Swap File

Before you start creating the swap file, confirm that you don’t already have swap space allocated using the top or free or swapon -s commands mentioned previously. You should also confirm that you have an EBS backed EC2 instance with ephemeral/instance storage mounted at /mnt (you can check this with the lsblk command).

We will be setting up a 2GB swap file for an m3.medium instance with 3.75GB of RAM. If you’re curious about how much swap file you need to create in your case, you can have a look at this link.

Now run this command:

1
sudo dd if=/dev/zero of=/mnt/swapfile bs=1M count=2048

That command will write 2GB worth of zeroes to the new file called “swapfile” on /mnt. Now set the permissions for “swapfile” with these two commands:

1
2
sudo chown root:root /mnt/swapfile
sudo chmod 600 /mnt/swapfile

Then run these two commands:

1
2
sudo mkswap /mnt/swapfile
sudo swapon /mnt/swapfile

The mkswap command converts the “swapfile” full of zeroes into an actual Linux swap file (swap space). The swapon command sets up “swapfile” as the system swap file.
 
In order to make sure that this swap space is mounted after a system reboot, you need to update the fstab file (located in /etc/fstab). Append this line using your preferred text editor:

1
/mnt/swapfile swap swap defaults 0 0

Now enable swapping with:

1
swapon -a

To confirm that the new swap file is in use, run the swapon -s command. To check the ammount of swap space, run either the top or the free commands.

NOTE: This swap space will not survive when the instance is stopped and later started because the ephemeral storage where the swap file is stored gets destroyed when the instance stops. However, the swap file will survive a reboot. If you must do a stop/start, remember to recreate the swap file using the instructions here with the exception of the fstab changes since those are stored in EBS.

Found this article valuable? Want to show your appreciation? Here are some options:

  1. Spread the word! Use these buttons to share this link on your favorite social media sites.
  2. Help me share this on . . .

    • Facebook
    • Twitter
    • LinkedIn
    • Reddit
    • Tumblr
    • Pinterest
    • Pocket
    • Telegram
    • WhatsApp
    • Skype
  3. Sign up to join my audience and receive email notifications when I publish new content.
  4. Contribute by adding a comment using the comments section below.
  5. Follow me on Twitter, LinkedIn, and Facebook.

Related

Filed Under: AWS, Azure, Cloud, Linux & Servers Tagged With: Amazon EC2, Linux, SwapFile, VirtualRAM

About Ehi Kioya

I am a Toronto-based Software Engineer. I run this website as part hobby and part business.

To share your thoughts or get help with any of my posts, please drop a comment at the appropriate link.

You can contact me using the form on this page. I'm also on Twitter, LinkedIn, and Facebook.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

25,641
Followers
Follow
30,000
Connections
Connect
14,582
Page Fans
Like

POPULAR   FORUM   TOPICS

  • How to find the title of a song without knowing the lyrics
  • The Art of Exploratory Data Analysis (Part 1)
  • Welcome Message
  • How To Change Or Remove The WordPress Login Error Message
  • Getting Started with SQL: A Beginners Guide to Databases
  • Replacing The Default SQLite Database With PostgreSQL In Django
  • How to Implement Local SEO On Your Business Website And Drive Traffic
  • Building a WordPress Site Part 3: Securing Your Website
  • How To Enable Private Browsing: Instructions For All Popular Browsers
  • How To Download YouTube Videos For Free
  • Recently   Popular   Posts   &   Pages
  • Actual Size Online Ruler Actual Size Online Ruler
    I created this page to measure your screen resolution and produce an online ruler of actual size. It's powered with JavaScript and HTML5.
  • Allowing Multiple RDP Sessions In Windows 10 Using The RDP Wrapper Library Allowing Multiple RDP Sessions In Windows 10 Using The RDP Wrapper Library
    This article explains how to bypass the single user remote desktop connection restriction on Windows 10 by using the RDP wrapper library.
  • WordPress Password Hash Generator WordPress Password Hash Generator
    With this WordPress Password Hash Generator, you can convert a password to its hash, and then set a new password directly in the database.
  • Forums
  • About
  • Contact

© 2021   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy