> For the complete documentation index, see [llms.txt](https://docs.glesys.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.glesys.com/platform/control-panel/api/real-world-use-cases/resize-a-vm-when-running-out-of-memory.md).

# Resize a VM when running out of memory

Using the Glesys API you can automatically resize a VM when it starts running low on memory.

***

The Glesys API can be used for many things, including upgrading the memory of a virtual server.

These scripts are ideally run from crontab every minute (adjust the scripts as needed). With a little extension, it’s easy to implement multi‑step upgrades and downgrades, send email notifications to the sysadmin on changes, upgrade the number of CPU cores, and so on.

If you modify these scripts or create completely different solutions, feel free to submit them to us so we can showcase them. **Well‑crafted scripts are rewarded with nice discounts!**

More information about the Glesys API can be found on [GitHub](https://github.com/glesys/api-docs/wiki).

## Increase the memory using Bash

Below, we show you how to create a script that checks how much memory is being used on the virtual server. When usage reaches 90%, the memory is upgraded to 4096 MB. If you schedule this as a cron job, you’ll get an automatic memory increase on your server whenever the memory limit is reached.

{% code title="/root/adjust-memory.sh" %}

```bash
#!/bin/bash
TOTAL=`cat /proc/meminfo |grep "MemTotal" |awk {'print $2'}`
FREE=`cat /proc/meminfo |grep -E "MemFree|Cached"| awk '{s+=$2} END {print s}'`
USAGE=$((100-FREE*100/TOTAL));

if [ $USAGE -gt 90 ]; then
       /usr/bin/curl -X POST -d serverid=wps123456&memorysize=4096 -k --basic -u cl12345:API-KEY https://api.glesys.com/server/edit/
fi
```

{% endcode %}

## Increase the memory using Ruby

Below is a slightly more advanced script. When the usage goes above 90%, the memory is increased to 4096 MB. When the usage goes below 20%, the memory is decreased to 2048 MB. If you schedule this as a cron job, you’ll get automatic memory increase and decrease on your virtual server based on its usage.

{% code title="/root/adjust-memory.rb" %}

```ruby
require "net/http"
require "net/https"
require "uri"

def change_memory(memory, username, api_key, server_id)
  params = { serverid: server_id, memorysize: memory }
  uri = URI.parse("https://api.glesys.com/server/edit")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri)
  request.basic_auth(username, api_key)
  request.set_form_data(params)
  response = http.request(request)
  if response.code == "200"
     puts "Memory changed to #{memory}"
   end
end

def memory_usage
  total_memory = `cat /proc/meminfo |grep -E "MemTotal"|awk {'print $2'}`.strip.to_f
  free_memory = `cat /proc/meminfo |grep -E "MemFree|Cached"| awk '{s+=$2} END {print s}'`.strip.to_f
  1 - (free_memory / total_memory)
end

if ARGV.size < 3
  abort("required arguments: username apikey serverid")
end

username = ARGV[0]
api_key = ARGV[1]
server_id =ARGV[2]

memory_low=2048
memory_high=4096

if memory_usage > 0.9
  puts "Increasing Memory"
  change_memory(memory_high, username, api_key, server_id)
elsif memory_usage < 0.2
  puts "Decreasing Memory"
  change_memory(memory_low, username, api_key, server_id)
end
```

{% endcode %}

### Running the Ruby script in a cron job

Make the script executable with `chmod +x /root/adjust-memory.rb`. Then, add the script to the crontab using `crontab -e`. To run the script every minute, add the following line to the crontab:

{% code title="crontab" %}

```
* * * * * /root/adjust-memory.rb cl12345 API-KEY wps12345
```

{% endcode %}
