To Optimize Your App on Heroku, Make Sure to Monitor Memory Usage
At the infrastructure level, there are several components to look into to optimize a Heroku app. These can include dyno size, CPU performance, process types, and memory usage. In this post, we’ll focus on monitoring memory usage as a key step in getting the most from a Heroku app. Specifically, we’ll go over the following topics:
- Understanding memory usage on Heroku
- Choosing the metrics so you can easily monitor memory usage
- Using an add-on to monitor memory usage
The Hosted Graphite add-on for Heroku by MetricFire enables you to monitor memory usage on your app quickly and easily. It combines the time-series data-handling capabilities of Graphite and robust data visualization features from Grafana into a ready-to-use add-on. Attach it to your app right away with a one-click installation.
Once you install the add-on, HG Heroku monitoring will automatically generate Heroku-specific dashboards for you, check it out:
Understanding memory usage on Heroku
To monitor memory usage at the dyno level, you need to gain visibility. This allows you to:
- Identify memory issues before they affect your app’s performance
- Ensure that the minimum required amount of memory is maintained
- Avoid exceeding memory quota for a given dyno plan
When your app uses more memory than what’s available in a dyno, the application encounters an R14 error. When this happens, Heroku allots swap memory to the process in order to make up for the deficit. But using swap memory tends to adversely affect your app’s response time. This is why monitoring memory consumption is an essential step in optimizing your app’s performance.
To monitor memory usage at the dyno level, you need to gain visibility. This allows you to:
- Identify memory issues before they affect your app’s performance
- Ensure that the minimum required amount of memory is maintained
- Avoid exceeding the memory quota for a given dyno plan
When your app uses more memory than what’s available in a dyno, the application encounters an R14 error. When this happens, Heroku allows swap memory to the process to make up for the deficit. However, using swap memory tends to adversely affect your app’s response time. This is why monitoring memory consumption is an essential step in optimizing your app’s performance.
Choosing the metrics to monitor for visibility into memory usage
Heroku’s memory usage metrics give insight into the total memory usage, swap usage, and memory quota for a process type (i.e., web or worker process). Heroku Labs’ log-runtime-metrics give visibility into memory consumption and swap with the following indicators:
- Resident Memory (memory_rss)
- Disk Cache Memory (memory_cache)
- Swap Memory (memory_swap)
- Total Memory (memory_total)
- Memory Quota (memory_quota)
- Pages Written to Disk (memory_pgpgout)
- Pages Read from Disk (memory_pgpgin)
If not yet available in your app’s metric dashboard, these metrics can be collected by enabling log-runtime-metrics:
$ heroku labs:enable log-runtime-metrics
Using an add-on for memory usage monitoring
Let’s take a look at how to provision and set up a Heroku add-on for memory usage monitoring. We use the Hosted Graphite Heroku add-on because it gives us the power of Graphite time-series data management and Grafana data visualization with minimal configuration and setup.
First, attach the add-on to your app:
$ heroku addons:create hostedgraphite
Once provisioned, the add-on will collect a base set of performance metrics (CPU, memory, and I/O metrics) and forward the data points to a Grafana dashboard. To make sure you’re tracking the full set of key memory usage metrics, enable log-runtime-metrics as indicated in the previous section.
The Hosted Graphite Heroku add-on automatically generates a Grafana dashboard called Heroku which displays the collected data points, including the memory usage metrics. The below screenshot shows the memory metrics for the app's single web dyno as displayed in the dashboard.
Another benefit of using the Hosted Graphite Heroku add-on is that you can easily include other metrics and create custom Grafana dashboards and alerts, without doing additional configuration. Hosted Graphite can handle collecting and storing a huge volume of time-series data as well as thousands of metric types.
Conclusion
You’re now ready to start optimizing your Heroku app with the help of robust memory usage monitoring. The next step is to gain more hands-on experience with tools like Hosted Graphite. Monitoring Heroku Memory usage has never been easier than with the Hosted Graphite Heroku monitoring add-on!