Post

EBS Memory Swap

EBS Memory Swap Guide

Swap space in Linux can be used when a system requires more memory than it has been physically allocated (RAM). When swap space is enabled, Linux systems can swap infrequently used memory pages from physical memory to swap space (either a dedicated partition or a swap file in an existing file system) and free up that space for memory pages that require high-speed access.

In order to do this guide, you will need to create an EBS Volume and attach the EBS Volume to your EC2 instance.

  1. Run lsblk to check new volume, it will appear as /dev/xvdc on instance.
    1
    2
    3
    4
    5
    
    $ lsblk
    NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    xvda    202:0    0  256G  0 disk
    └─xvda1 202:1    0  256G  0 part /
    xvdc    202:80   0   32G  0 disk
    
  2. Allow access to volume.
    1
    
    sudo chmod 600 /dev/xvdc
    
  3. Set up the swap area.
    1
    
    sudo mkswap /dev/xvdc
    
  4. Enable swap.
    1
    
    sudo swapon /dev/xvdc
    
  5. Edit /etc/fstab to make the settings persist.
    1
    
    sudo nano /etc/fstab
    

    Add the following line:

    1
    
    /dev/xvdc none swap sw 0 0
    
  6. Reboot.
    1
    2
    3
    
    sudo reboot
    lsblk
    df
    
  7. Check swap space.
    1
    2
    3
    
    sudo swapon --show
    NAME      TYPE      SIZE   USED PRIO
    /dev/xvdf partition  32G 279.9M   -1
    
This post is licensed under CC BY 4.0 by the author.