Getting 5gb of RAM on your AWS EC2 free-tier instance
SICKEE!! I lured you into a how-to create swapfile tutorial. So lets do it.
-
Create 4gb empty file
sudo fallocate -l 4G /swapfile
-
Fill it with zeroes
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
-
Set the persmissions
sudo chmod 600 /swapfile
-
Format that file as swap
sudo mkswap /swapfile
-
Enable the swap file
sudo swapon /swapfile
-
Run
htop
and see if you have more “ram” -
Add this:
/swapfile none swap sw 0 0
to the/etc/fstab
file, so it will be persistent after reboot.
Thats it!
notes
This is going to be very, very slow “ram”, around 200 MB/s, normal ddr4 ram is 20 GB/s.
It is still worth getting it tho, this stops your server from crashing in case you need more than 1gb, while i was compiling some program it needed more than 1gb and the server crashed, i had to reboot it on the aws pannel, it caused some downtime, if i had swap, this would not have happened.
And even if you have like 16gb of ram, the system will still use some swap even tho you have lots of ram to spare, i guess it is for some processes that need to stay in memory but dont change that much.
(as you can see, there is still a lot of ram left, but the system still uses swap)
BTW, if you are abusing this on a crap chinese ssd, usb drive, micro sd card (something like a raspi pi) , using a lot of swap, it will blow the drive, happened to me using a lot of swap on a raspi pi (sd card) and cheap kingspec ssd.