How to Increase RAM on a DigitalOcean VPS?

Hi, colleague! Stuck with insufficient RAM on your DigitalOcean VPS? I know, it’s a pain. I spent a lot of time figuring this out myself, and believe me, it’s not as scary as it seems. In this article, I’ll walk you through how to increase RAM on your server step-by-step and share some tips to help you avoid common mistakes. Get ready for a mind-blowing experience – this is going to be awesome!

Checking Current RAM
How to Increase RAM on a DigitalOcean VPS? - Screenshot of `free -h` command output

Okay, so the first thing we need to do is find out how much RAM you currently have. It seems obvious, but believe me, I’ve seen people spend hours trying to solve the problem without even knowing what they’re dealing with. The simplest way is the free -h command. Run it in the terminal:

free -h

You’ll see something like this:

              total        used        free      shared  buff/cache   available
Mem:           1024Mi       200Mi       700Mi        10Mi       124Mi       880Mi
Swap:          2048Mi         0Mi      2048Mi

Here, Mem: is the RAM. Pay attention to used and available memory. If used is close to total, then yes, you definitely need more RAM.

VPS хостинг

Виртуальные серверы с гарантированными ресурсами

Выбрать VPS

Another way is top. This command shows dynamic system resource usage in real-time. Press q to exit.

top

Pro tip: use htop – it’s an interactive version of top, much more convenient for monitoring.

sudo apt update && sudo apt install htop

Choosing a More Powerful VPS Plan
How to Increase RAM on a DigitalOcean VPS? - Screenshot of DigitalOcean Droplet creation page with RAM options

The easiest (though not always the cheapest) way to increase RAM is to choose a more powerful VPS plan in DigitalOcean. Go to the control panel, select your Droplet, and click «Upgrade». DigitalOcean will offer you several options with different amounts of RAM, CPU cores, and disk space. Choose what you need and confirm the changes. This will reboot your server.

Word of warning: be careful with this method. Upgrading to a plan with a larger amount of resources can be significantly more expensive. Consider whether you really need that much RAM or if you can get by with optimization.

Here are some example commands you can use to check your current plan:

doctl compute droplet get <droplet_id>
cat /proc/cpuinfo  # Processor
cat /proc/meminfo # Memory

(Replace <droplet_id> with your Droplet ID)

Vertical Scaling with Additional Resources

If you don’t want to change your entire plan right away, you can try vertical scaling. This means adding additional resources to your existing plan. In DigitalOcean, this can be done by increasing the amount of RAM, CPU cores, or disk space. This method can be more economical than changing the entire plan.

Unfortunately, DigitalOcean doesn’t offer the ability to increase RAM without changing the entire plan directly through the control panel. Therefore, this step boils down to the previous point. However, after choosing a more powerful plan, you can check the correctness of the settings:

sudo reboot
free -h

After rebooting the server, check the RAM amount using the free -h command. Boom! If everything went well, you’ll see the increased amount of available memory.

Optimizing RAM Usage

Real talk: sometimes you don’t need to buy more RAM. Sometimes it’s enough to optimize what you already have. This method requires more time and knowledge, but it can save money.

Here are a few tips:

  • Close unnecessary processes: Use top or htop to find processes consuming a lot of memory and terminate them using kill (be careful!).
  • Clear the cache: The sudo apt autoremove command will remove unnecessary packages and free up space.
  • Remove unused files: sudo apt autoclean will remove old packages.
  • Optimize databases: If you’re using a database, make sure it’s optimized. This can significantly reduce memory consumption.
  • Use swap: A swap partition is a file on the hard drive that is used as additional memory. However, working with swap is slower than with RAM. Therefore, it’s better to optimize RAM usage than to rely on swap.

Example of using kill (be careful!):

ps aux | grep process_name  # Find the process PID
kill <PID>

(Replace process_name with the process name and <PID> with its ID)

Monitoring Memory Usage

After you’ve increased RAM or optimized resource usage, it’s important to monitor how memory is being used. For this, you can use various monitoring tools such as Grafana, Prometheus, or even built-in Linux tools.

Here are a few commands for basic monitoring:

free -h
top
htop

More advanced tools, such as Grafana, allow you to create dashboards to visualize data on memory consumption and other resources. This helps to quickly identify problems and prevent memory shortages.

To install Grafana use:

sudo apt update && sudo apt install grafana

(Remember, this is just an example of installation. Configuring Grafana is a separate, large topic)

Troubleshooting Memory Issues

If, after all the steps taken, the problem of insufficient memory remains, then you need to dig deeper. You may have memory leaks in applications or inefficient code. This will require profiling and a more in-depth analysis.

Here are some commands that might help:

dmesg | tail
journalctl -xe
systemctl status <service_name>
ps aux | grep <process_name>

These commands will help you find error messages, check the status of services, and see which processes are consuming a lot of resources. If nothing helps, refer to your application’s documentation or seek help on Stack Overflow. https://stackoverflow.com/

Remember, debugging is an art, not a science. Be patient, and you will definitely find a solution!

“The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.” — Brian W. Kernighan

I hope this article helped you increase RAM on your DigitalOcean VPS! Good luck!