VSCode Keeps Disconnecting? How to Stabilize Your SSH Connection on Low-Spec Servers with a Swap File

table of contents

✅ Introduction

While articles about AI and high-performance GPUs are everywhere these days, many users are actually working with low-spec environments like free-tier Ubuntu serverse2-micro (GCP), and Oracle Cloud’s Free Tier.

I’m one of them. When I was working with a VSCode SSH connection, it would suddenly disconnect in the middle of a session. Sometimes my work-in-progress code would be lost, and I had experiences where I was forced to switch back to TeraTerm.

🧪 Current Machine Specs and Problems

Here are the specs for the Ubuntu instance I’m using:

$ free
              total        used        free      shared  buff/cache   available
Mem:          979708      526436       98036        1556      536932      453272
Swap:              0           0           0
  • About 1GB of memory
  • No Swap configured
  • VSCode disconnects during long sessions

In this state, just running a slightly heavy process can trigger an “Out of Memory (OOM)” error, causing the VSCode connection to time out and disconnect.

🛠 Solution: Introducing a Swap File

By setting up a Swap file, you create an emergency refuge for when physical memory runs out. Here are the steps:

1. Create a swap file (e.g., 1GB)

sudo fallocate -l 1G /swapfile

Or, for better compatibility:

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

Output:

sudo dd if=/dev/zero of=/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 17.8276 s, 60.2 MB/s

2. Set permissions

sudo chmod 600 /swapfile

3. Initialize and enable

sudo mkswap /swapfile
sudo swapon /swapfile

Output:

sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=ea117786-54d3-4317-b575-130df8e58c5b

4. Make it persistent (enable after reboot)

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Output:

/swapfile none swap sw 0 0

5. Check for duplicate lines in /etc/fstab

cat /etc/fstab

Make sure a line like the one below is not written more than once:

/swapfile none swap sw 0 0

❌ Bad Example (What I had):

/swapfile none swap sw 0 0
/swapfile none swap sw 0 0

✅ Good Example:

/swapfile none swap sw 0 0

🔧 How to fix duplicates:

Open the file with:

sudo nano /etc/fstab

or

sudo vi /etc/fstab

Then, delete one of the duplicate lines and save the file.

💡 Why is this important?

  • Having duplicate lines in fstab can cause swap area duplication errors or mount failures during boot.
  • This is especially true for cloud-init based instances (e.g., GCP/Oracle) where the boot process is sensitive.

🔍 What happened after?

My VSCode SSH connection became extremely stable, and I could go back to my normal development workflow. It’s especially perfect for tasks where you don’t need AI-level resources but just want to write some simple Python scripts.

💬 Summary

VSCode disconnections aren’t just caused by unstable connections; they can also be related to insufficient memory on the server side.

Even on e2-micro or Oracle Cloud’s free tier, you can create a sufficiently comfortable development environment by setting up a swap file. “A dramatic improvement with a little tweak”—this kind of down-to-earth improvement should be more valuable than ever in the times to come.

Swap File Creation Guide

🔧 Swap File Creation Guide

Solution for VS Code Remote-SSH Disconnection Issues
⚠️ Current Situation: 1GB RAM Environment
1GB
Total Memory
0MB
Swap Space
500MB+
VS Code Required
💥
OOM Killer Triggered
💡 Solution: Create Swap File
Supplement physical memory with disk space when needed

📋 Swap File Creation Steps

1
Create Swap File
Create a 1GB swap file rapidly. Using the fallocate command allows instant disk space allocation.
sudo fallocate -l 1G /swapfile
fallocate: Instantly allocates disk space
-l 1G: Specifies 1GB size
/swapfile: Path to the swap file
2
Set File Permissions
For security reasons, restrict access to the swap file so that only root can read and write to it.
sudo chmod 600 /swapfile
chmod 600: Only owner (root) can read/write
Swap files may contain sensitive information, so this is essential
3
Initialize as Swap Space
Format the created file so that Linux can recognize it as swap space.
sudo mkswap /swapfile
mkswap: Formats file as swap space
At this point, it’s not yet active as swap
4
Enable Swap
Activate the formatted swap file as actual system swap space.
sudo swapon /swapfile
swapon: Enables swap space
From this moment, the system starts using swap
🔄 Make Persistent After Reboot
Add the following line to /etc/fstab file:
/swapfile none swap sw 0 0
📝 Edit command example: sudo nano /etc/fstab
Performance Considerations
SSD environments: Swap usage has minimal performance impact
HDD environments: Consider using zram (RAM compression) for better performance
Recommendation: Even a small swap is better than none – prevents sudden application termination
Verify Swap Creation
free -h
swapon –show
cat /proc/meminfo | grep Swap
Success if you see: Swap: 1.0G
🎉 VS Code Remote-SSH Now Runs Stable!
Free from sudden disconnections caused by OOM Killer
Your free-tier VPS is now ready for serious development work

🧩 Bonus: First Aid for VSCode Disconnections

  • Close and reopen the VSCode SSH session.
  • If necessary, delete the .vscode-server directory and let it rebuild.
  • For long-running tasks, using screen or tmux is also highly recommended.

If you like this article, please
Follow !

Please share if you like it!
table of contents