Why a 2GB VPS Needs Swap Space for Spring Boot Applications
Many developers believe that swap should be disabled because it is slower than RAM.
While that is technically true, running a small VPS without swap can be dangerous.
My Environment
- Ubuntu Server
- Spring Boot
- PostgreSQL
- Nginx
- 2GB RAM
The server worked well most of the time.
However, during memory spikes, Linux started killing the Java process.
What Happens Without Swap
When physical memory becomes exhausted, Linux must free memory quickly.
Without swap, the kernel may activate the Out Of Memory (OOM) Killer.
The OOM Killer selects a process and terminates it.
In many cases, Java applications become the target.
Why Swap Helps
Swap gives Linux an additional memory buffer.
Inactive memory pages can be moved from RAM to disk.
This is slower than RAM but much better than terminating a production application.
Recommended Swap Size
For small VPS servers:
RAM Swap 1GB 1GB - 2GB 2GB 2GB 4GB 2GB - 4GB 8GB+ 2GB - 4GB
Final Thoughts
Swap is not a replacement for RAM.
However, for small production servers running Spring Boot, PostgreSQL, and Nginx, swap can dramatically improve stability.
A server with 2GB RAM and 2GB swap is usually much safer than a server with 2GB RAM and no swap.