Add Swap Memory t3.micro
December 3, 2024 · AI Conversations
### **1. Check Current Swap**
sudo swapon --show
If there’s no output, swap is not enabled.
### **2. Check Available Disk Space**
df -h
Ensure there’s enough space on your instance to create a swap file.
### **3. Create a Swap File**
For a 1GB swap file:
sudo fallocate -l 1G /swapfile
If `fallocate` isn’t available, use:
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
### **4. Set Correct Permissions**
sudo chmod 600 /swapfile
### **5. Set Up the Swap Area**
sudo mkswap /swapfile
### **6. Enable the Swap File**
sudo swapon /swapfile
### **7. Verify Swap**
sudo swapon --show
free -h
### **8. Make Swap Persistent Across Reboots**
Edit the `/etc/fstab` file:
sudo nano /etc/fstab
Add the following line:
/swapfile swap swap defaults 0 0
Save and exit (`Ctrl + O`, `Enter`, `Ctrl + X`).
### **9. Optional: Adjust Swappiness**
Swappiness determines how often the system uses swap. Lower values reduce swap usage, preserving performance. Check the current value:
cat /proc/sys/vm/swappiness
Set it to a lower value (e.g., 10):
sudo sysctl vm.swappiness=10
Make it permanent by adding to `/etc/sysctl.conf`:
vm.swappiness=10
### **10. Optional: Adjust Cache Pressure**
Reduce cache pressure for better performance:
sudo sysctl vm.vfs_cache_pressure=50
Make it permanent by adding to `/etc/sysctl.conf`:
vm.vfs_cache_pressure=50
### **Cleanup if Needed**
To disable and remove the swap file:
sudo swapoff /swapfile
sudo rm /swapfile
This will effectively add swap memory to your t3.micro instance while keeping within the AWS free tier limitations.