Memory Tuning for Spring Boot on a 2GB Ubuntu VPS
Running Spring Boot on a small VPS requires careful memory management.
After several production incidents, I settled on a simple configuration that works well for my personal blog.
Current Configuration
java \ -Xms256m \ -Xmx512m \ -XX:+UseG1GC \ -jar blog.jar
Systemd configuration:
MemoryLimit=1200M CPUQuota=80%
Why I Use Xmx 512MB
A common mistake is allowing Java to consume too much memory.
On a 2GB VPS, the operating system, PostgreSQL, and Nginx also need resources.
By limiting Java to 512MB heap, I ensure that the entire server remains responsive.
Monitoring Memory
Useful commands:
free -h
ps aux --sort=-rss | head -20
journalctl -k | grep -i oom
These commands quickly reveal memory pressure and OOM events.
Conclusion
For small personal projects, simplicity wins.
A 2GB VPS, Spring Boot with Xmx 512MB, PostgreSQL, Nginx, and a 2GB swap file provide a stable and affordable setup.