(For the Korean version, click here)
Introduction: Why Services Stop Without Warning
When operating AWS Lightsail or small cloud instances (with 1GB to 2GB of RAM), the most common issue you’ll face is the ‘DB going down due to out-of-memory’ errors. (Though I haven’t personally encountered this on AWS Lightsail yet!) When available memory is exhausted, the Linux kernel triggers the OOM (Out of Memory) Killer to protect the system by forcibly terminating specific processes—and the targets are usually memory-heavy engines like MySQL or MariaDB.
While scaling up server specifications is the best solution, it can be a burden for personal projects or test servers where cost efficiency is key. This is where Swap comes in. By setting up part of your hard disk to act as virtual RAM, you can ensure your server doesn’t crash immediately during temporary traffic spikes.
Checking Current Swap Status (Before)
Before we begin, check the current memory and swap status of your system. In a default configuration, the swap area will likely be displayed as zero.
Bash
# Check memory status (-h: display in human-readable format)
free -h
[Checkpoint] Check the Swap: entry in the output. If it shows 0B or 0, your server is currently in a risky state where it will terminate processes immediately upon running out of memory.

Creating the Swap File
First, allocate a 1GB empty file within the system to be used as swap space. We use the dd command to write 1MB chunks 1024 times to create exactly 1GB.
Bash
# Create a 1GB file named /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

Setting Permissions for Security
Since sensitive system data can be recorded in the swap file, security settings are crucial. Access to this file must be strictly restricted so that only the root account can access it.
Bash
# Grant read/write permissions only to the owner (root)
sudo chmod 600 /swapfile

Initializing and Activating the Swap Area
Once the file is created, format it so that Linux recognizes it as swap space and then activate it.
Bash
# 1. Format the created file as swap area
sudo mkswap /swapfile
# 2. Activate it as system swap space
sudo swapon /swapfile

Checking and Monitoring the Results
Verify that the swap capacity has been successfully added. Check if the numbers in the Swap section of the free -h command have increased.
Bash
# Verify overall memory and swap status
free -h
# Check the list of currently active swap files
swapon -s
If you see about 1.0G of swap space in the results, the setup is successful.

Encountered a situation where the swapon -s command didn’t work because the package was not installed… In this case, you can fix it by installing the util-linux package.
sudo apt-get install util-linux
Setting Auto-Activation After Reboot
The settings above are temporary and will disappear if the server is rebooted. To ensure the 1GB swap is automatically enabled every time the server starts, you must register it permanently in the /etc/fstab file.
Bash
# Open the fstab file for editing
sudo vi /etc/fstab
Add the following line to the very bottom of the file and save it:
/swapfile swap swap defaults 0 0

💡 Engineer’s Note: The Last Line of Defense Behind Convenience
Living in an era of Big Data where large capacities (16GB–128GB) are the standard, even I used to overlook the importance of Swap. However, it’s a completely different story in low-spec cloud environments. While Swap isn’t as fast as physical RAM, it is a hundred times better than being woken up by a server-down alarm at 3 AM. My Lightsail instance is quite ‘small and cute,’ so I’m going to start by adding exactly 1GB as a minimal lifeline.
Conclusion
You now have a reliable 1GB virtual memory shield. This should prevent sudden service interruptions even under moderate load. Never forget that infrastructure operation begins with preparing for the ‘worst-case scenario.’