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
» 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.
As explained in Heroku's docs, Dynos are simply lightweight Linux containers dedicated to running your application processes. At the most basic level, a newly deployed app to Heroku will be supported by one Dyno for running web processes.
Use jemalloc to reduce Rails memory usage On Heroku, you can add jemalloc using a buildpack. For my app, it resulted in ~20% memory usage decrease. Just make sure to test your app with jemalloc thoroughly on a staging environment before deploying it to production.
Heroku provides enough free hours to run a single dyno continuously for a month, but if we need a worker dyno for background processing (most apps do), we will not have enough free dyno hours. Free dynos are for great for demos, experimentation, and perhaps a staging app. For production apps, we need to pay.
Automatic dyno restartscreate a new release by deploying new code. change your config vars. change your add-ons. run heroku restart.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With