Search code examples
performanceherokuruby-on-rails-4unicorn

When do Heroku dynos use swap memory?


I'm hosting my Rails 4.1.4 project with 2 Unicorn processes on free dyno for my development server. After the app running for a while, sometimes I feel getting slow. I added New relic, logentries, and enable log-runtime-metrics. Then I look at New relic and logentries

enter image description here

» heroku web.1  - - source=web.1 dyno=heroku.21274089.82eb32b4-c547-4041-b452-d3fedae05ee9 sample#load_avg_1m=0.00 sample#load_avg_5m=0.00 sample#load_avg_15m=0.01
» heroku web.1  - - source=web.1 dyno=heroku.21274089.82eb32b4-c547-4041-b452-d3fedae05ee9 sample#memory_total=393.41MB sample#memory_rss=368.38MB sample#memory_cache=4.47MB sample#memory_swap=20.56MB sample#memory_pgpgin=121244pages sample#memory_pgpgout=25796pages

What I don't understand is my dyno’s memory is only sample#memory_rss=368.38MB, but why it already uses swap memory sample#memory_swap=20.56MB? Because from what I thought from heroku article https://devcenter.heroku.com/articles/dynos#memory-behavior, it should switch to swap memory if it reaches dyno's memory which is 512 Mb for free dyno.


Solution

  • I was seeing significant swap even when using only 50% of available RAM in my app, so I asked. Here's a quote from their support team:

    We leave Ubuntu's swappiness at its default of 60 on our runtimes, including PX dynos.

    A value of 60 ensures that your app will begin swapping well before it reaches max memory. The Linux kernel parameter vm.swappiness ranges from 0 to 100, with 0 indicating no swap and 100 indicating always swap.

    So when running on Heroku you should expect your application to swap even if the footprint of your app is far smaller than the advertised RAM of your dyno.