353

I'm currently running an ec2 micro instance and i've been finding that the instance occasionally runs out of memory.

Other than using a larger instance size, what else can be done?

2

11 Answers 11

645

A fix for this problem is to add swap (i.e. paging) space to the instance.

Paging works by creating an area on your hard drive and using it for extra memory, this memory is much slower than normal memory however much more of it is available.

To add this extra space to your instance you type:

sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo chmod 600 /var/swap.1
sudo /sbin/mkswap /var/swap.1
sudo /sbin/swapon /var/swap.1

If you need more than 1024 then change that to something higher.

To enable it by default after reboot, add this line to /etc/fstab:

/var/swap.1   swap    swap    defaults        0   0
11
  • 41
    if means 'input file', of means 'output file', bs means 'block size' and count is the number of blocks you want to allocate… you can read the man page of the command for more info: linux.die.net/man/1/dd Commented May 19, 2014 at 22:23
  • 7
    That dd command line means "copy from /dev/zero to /var/swap.1. Do that by reading 1024 blocks of size 1 megabyte". It's a quick way to create a 1GB file full of zeroes.
    – Nelson
    Commented Jan 21, 2015 at 4:43
  • 4
    enough old, but my swap memory remain 0% used even after restart, any help? Commented Mar 31, 2017 at 8:46
  • 4
    If you want to check if the swap is active: $> free -m
    – herve
    Commented May 24, 2017 at 18:10
  • 4
    You can use fallocate which is almost instantaneous (baeldung.com/linux/create-large-file) instead of dd which can take a while with multiple GB files. fallocate -l 20G /swapfile works great on an EKS AMI. Commented Jan 13, 2023 at 1:30
245

Swap should take place on the Instance Storage (ephemeral) disk and not an EBS device. Swapping will cause a lot of IO and will increase cost on EBS. EBS is also slower than the Instance Store and the Instance Store comes free with certain types of EC2 Instances.

It will usually be mounted to /mnt but if not run

sudo mount /dev/xvda2 /mnt

To then create a swap file on this device do the following for a 4GB swapfile

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

Make sure no other user can view the swap file

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

Make and Flag as swap

sudo mkswap /mnt/swapfile
sudo swapon /mnt/swapfile

Add/Make sure the following are in your /etc/fstab

/dev/xvda2      /mnt    auto    defaults,nobootwait,comment=cloudconfig 0   2
/mnt/swapfile swap swap defaults 0 0

lastly enable swap

sudo swapon -a
17
  • 7
    this is definitely the most comprehensive answer imo
    – Jon B
    Commented May 8, 2014 at 0:33
  • 4
    To mount a file system one must specify the file system type. To find the file system the command is: $df -T This will show you the devices and their file systems. Mostly ext4. For me it was /dev/xvda1, not 2. Commented May 10, 2014 at 15:31
  • 11
    According to docs.aws.amazon.com/AWSEC2/latest/UserGuide/… it is not longer possible to add Instance stores to t1/t2 instances. After trying regardless, it would seem to be correct.
    – muttonUp
    Commented Aug 28, 2014 at 0:58
  • 3
    @alfetopito You can probably create a literal swapfile on an existing partition instead of adding another partition as swap. A nice guide for Centos 6 - digitalocean.com/community/tutorials/…
    – muttonUp
    Commented Feb 26, 2015 at 15:10
  • 2
    "Swapping will cause a lot of IO and will increase cost on EBS." This might be the case for certain volume types, but not gp2, which only changes for storage. From the docs: "I/O is included in the price of the volumes, so you pay only for each GB of storage you provision." Latency concerns aside, it seems that cost might not be a concern when it comes to setting up EBS-backed swap volumes. Commented Feb 6, 2018 at 19:25
46

You can add a 1 GB swap to your instance with these commands:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
sudo mkswap /swapfile
sudo swapon /swapfile

To enable it by default after reboot, add this line to /etc/fstab:

/swapfile swap swap defaults 0 0
4
  • But from which of my filesystem it will take the space to create Swap?
    – Hussain
    Commented Feb 4, 2016 at 6:19
  • @Hussain It will create the swap in the root filesystem since the path is /swapfile. But you could also mount another filesystem and create the swap file there, e.g. /mnt/blah/swapfile. Commented Feb 4, 2016 at 15:10
  • Can confirm this works on a t2.micro instance. I did mine @ /mnt/swap though as I like a clean root. Commented Feb 5, 2016 at 20:38
  • 3
    Upvoted this answer because it is the shortest and does the job. However, swapfile chmod 600 command should be added.
    – Sõber
    Commented Jun 27, 2019 at 17:44
43

After applying the steps mentioned by ajtrichards you can check if your amazon free tier instance is using swap using this command

cat /proc/meminfo

result:

ubuntu@ip-172-31-24-245:/$ cat /proc/meminfo
MemTotal:         604340 kB
MemFree:            8524 kB
Buffers:            3380 kB
Cached:           398316 kB
SwapCached:            0 kB
Active:           165476 kB
Inactive:         384556 kB
Active(anon):     141344 kB
Inactive(anon):     7248 kB
Active(file):      24132 kB
Inactive(file):   377308 kB
Unevictable:           0 kB
Mlocked:               0 kB

SwapTotal: 1048572 kB

SwapFree: 1048572 kB

Dirty:                 0 kB
Writeback:             0 kB
AnonPages:        148368 kB
Mapped:            14304 kB
Shmem:               256 kB
Slab:              26392 kB
SReclaimable:      18648 kB
SUnreclaim:         7744 kB
KernelStack:         736 kB
PageTables:         5060 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     1350740 kB
Committed_AS:     623908 kB
VmallocTotal:   34359738367 kB
VmallocUsed:        7420 kB
VmallocChunk:   34359728748 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:      637952 kB
DirectMap2M:           0 kB
0
18

If you are on t2 instances (t2.micro, t2.medium, t2.small), there is no ephemeral or instance storage available to you. So you need to just create your swap in EBS which depending on your use case may or maynot be a good idea. Otherwise follow @David 's answer, and create your swap on the ephemeral storage to avoid paying EBS costs.

More info: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html there is a table that shows how much ephemeral storage you get for each instance type.

4
  • 1
    Can confirm this is generally a bad idea. If you have a process that goes rogue (I've had a node server do that, and a python/celery setup), you'll incur a ton of charges when it reads and writes to your EBS block all week without you knowing :-)
    – ZachM
    Commented Mar 29, 2016 at 15:24
  • 5
    According to aws.amazon.com/ebs/pricing , IO is included in all EBS volumes except Provisioned IPOS SSD (io1). You only per GB. This could have been a recent change in the pricing model - but it means that even though EBS is much slower than true Instance Storace, there are no additional charges for reads and writes.
    – bobsoap
    Commented Sep 27, 2016 at 15:48
  • @bobsoap Would you mind to clarify that ? I've checked your link, and found this For example, if you provision a volume with 1000 IOPS, and keep this volume for 15 days in a 30 day month, then in a Region that charges $0.10 per provisioned IOPS-month, you would be charged $50 for the IOPS that you provision ($0.10 per provisioned IOPS-month * 1000 IOPS provisioned * 15 days/30). You will be charged for the IOPS provisioned on a volume even when the volume is detached from an instance.. Which seems that AWS will charge for read and write operations. Commented Feb 6, 2017 at 9:18
  • 4
    @ValterHenrique The text you quoted applies to volumes with provisioned IOPS only, but you have the choice between several dfferent volume types. If you are using a "normal" volume without provisioned IOPS (like the general-purpose SSD volume type called "gp2"), read and write operations are not charged extra. Maybe a better link to compare: aws.amazon.com/ebs/details
    – bobsoap
    Commented Feb 8, 2017 at 9:35
17

Using David's Instance Storage answer initially worked for me (on a m5d.2xlarge) however, after stopping the EC2 instance and turning it back on, I was unable to ssh in to the instance again.

The instance logs reported: "You are in emergency mode. After logging in, type "journalctl -xb" to view system logs, "systemctl reboot" to reboot, "systemctl default" or "exit" to boot into default mode. Press Enter for maintenance"

I instead followed the AWS instructions in this link and everything worked perfectly, including after turning the instance off and on again.

https://aws.amazon.com/premiumsupport/knowledge-center/ec2-memory-swap-file/

sudo dd if=/dev/zero of=/swapfile bs=1G count=4

sudo chmod 600 /swapfile

sudo mkswap /swapfile

sudo swapon /swapfile

sudo swapon -s

sudo vi /etc/fstab
/swapfile swap swap defaults 0 0
9

You can create swap space using the following steps Here we are creating swap at /home/

  1. dd if=/dev/zero of=/home/swapfile1 bs=1024 count=8388608
    Here count is kilobyte count of swap space

  2. mkswap /home/swapfile1

  3. vi /etc/fstab
    make entry :
    /home/swapfile1 swap swap defaults 0 0

  4. run:
    swapon -a

4

We can add swap space in any server

create a file using dd command

 #dd if=/dev/zero of=/swapfile bs=1M count=2048
                    or
 #dd if=/dev/zero of=/swapfile bs=1024M count=2

bs is blocksize and count refers to the size in MB or GB

we can use vice versa

After creation change the permission of file :

 #chmod 600 /swapfile 

Then makeswap the file :

 #mkswap /swapfile 

Then enable the swap file with swapon command :

 #swapon  /swapfile 

Check with free command whether swap is enabled or not :

 #free -h
 #swapon -s
1

Try swapspace http://pqxx.org/development/swapspace/

Most distros have it packaged.

On EC2 you might want to change "swappath" to /mnt or high-iops disk.

1
  • 1
    From the docs: "In its current form, Swapspace is probably not a good choice for systems that need to remain responsive at all times;" Commented Jun 17, 2017 at 4:51
-1

You can use EFS to tackle this storage problem on EC2. To do this-

  1. Create EFS Volume
  2. Mount it to the directory of application, you're using on your EC2.

EFS is a scalable, fully managed file storage service. Refer to this link https://aws.amazon.com/efs/ for more info

-6

You can use the following script to add swap on Amazon Linux.

https://github.com/chetankapoor/swap

Download the script using wget:

wget https://raw.githubusercontent.com/chetankapoor/swap/master/swap.sh -O swap.sh

Then run the script with the following format:

sh swap.sh 2G

For a complete tutorial you can visit:

https://installvirtual.com/shell-script-to-create-swap/

3
  • I don't see what this answer contributes, besides just being a shellscript that basically does what the accepted answer already explained. Commented Aug 1, 2018 at 8:57
  • I just created a shell script it saves time so there will be 2 steps to create swap instead of running 5 commands and if swap already exists it will show you swap is already there. Commented Aug 1, 2018 at 12:44
  • 5
    Exactly. A shellscript that I am supposed to run as root. Made by someone, somewhere, that might change or disappear without a notice. All to save 4-5 command lines that were posted and accepted 5 years ago. Commented Aug 1, 2018 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.