puma: Memory Leak in rails app with ruby 2.3.0 and Puma 2.15.3

Hi all,

For some time I’ve been having a memory leak in my application. The current setup runs with Ruby 2.3.0, Rails 4.2 and Puma Webserver puma 2.15.3 It is recommended by Heroku to use Puma, however a lot of debugging has lead me to believe it’s actual Puma Webserver which is the cause of all the troubles. During debugging I’ve removed most of the gems, code etc but the issue kept showing up, RAM Memory increasing until an out of memory showed up. This is why I’m 99,999% sure the leak isn’t from my application. Next I installed “Thin” Webserver on my Development system and also this was not a solution. Then I also upgraded my ruby version to 2.3.0 and the leak was immediately solved, RAM usage stayed ok.

Due to this evolution, I upgraded my production system to Ruby 2.3.0 (thinking it was only a ruby version issue) but still I’m having the memory leak. I’m now at the point that it seems to be a combination of my Puma Webserver, together with Ruby 2.3.0…

Does anyone of you have any experience with Puma giving memory leaks with Ruby 2.3.0.

Thanks in advance, T

UPDATE: my puma.rb

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 30 (16 by maintainers)

Most upvoted comments

@ThomasCelen have you found out what the problem is?

Here’s a primer http://www.schneems.com/2015/05/11/how-ruby-uses-memory.html

No, GC should not reduce memory. Up to Ruby 2.3 it will not free aggressively, or not enough to matter for most apps. It also takes a larger memory overhead before it would start to reclaim memory. There is a GC setting for 2.4 that can be set, but it won’t impact this use case.

Even if the app isn’t doing any thing there are still event loops that create objects and do things like set timeouts, wait for incoming requests, etc. Not just from puma but other places in your code or libraries can run loops in new threads.

Your seeing memory grow and then level off. It’s totally normal.

@ThomasCelen that looks like pretty typical behavior of a rails app

the thrashing around and then slow growth could be from a lot of things

to get a more focused look at the “real” memory growth, try this:

leave that running for a day or two, the graph should be more informative.

and may as well update to ruby 2.3.1, if possible.

@ThomasCelen do you have any stats/graphs of the memory usage you’re seeing? I’ve running some tests and I do see it increase in small amounts but it appears to drop back down once a GC is done.