Few months back we upgraded on of our services from Ruby 2.6.6 with Rails 6.0.5.1 to Ruby 3.2.2 with Rails 7 and have noticed a slow but gradual increase of memory without any major change to the underlying application code.
We tried Ruby 3.2.2 with Rails 6 and the result is same as with Ruby 3.2.2 with Rails 7.
Service is simple background scanning service running Sidekiq only with some crons, no web traffic served.
CPU performance did improve
even the base memory usage improved but the memory keeps on increasing slowly every few hours until it reaches the ceiling
With Ruby 2, the service stabilized at a certain memory, with Ruby 3 it keeps on climbing. Then we used Jemalloc and the climbing slowed down but still happens after few days.
- Enabling and disabling JYIT Ruby 3 doesn’t have any impact.
- Used both Alpine and base ruby image, doesn’t have any impact
There is no memory bloat, we don’t see sharp surge in sidekiq memory but slow gradual increase
Slow increase with Ruby 3
Stable memory consumption with Ruby 2
Old Gemfile
ource 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.6'
gem 'dotenv-rails'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.5', '>= 6.0.5.1'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '>= 5.6.4'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.10'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
gem 'redis', '~> 4.2.5'
gem 'hiredis', '~> 0.6.3'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.7.3', require: false
gem 'sidekiq', '~> 6.2.1'
# Auto throttle jobs
gem 'sidekiq-throttled', '~> 0.13.0'
gem 'redis-namespace', '~> 1.8.0'
# For interacting with Github API
gem 'octokit', '~> 4.21.0'
gem 'faraday-http-cache', '~> 2.2.0'
# For docker api
gem 'docker-api', '~> 2.0.0'
# Collect metrics
gem 'dogstatsd-ruby', '~> 4.8.2'
# Asana
gem 'asana', '~> 0.10.2'
gem 'httparty', '~> 0.18.1'
gem 'shodanz', '~> 2.0.4'
gem 'redlock', '~> 1.2.1'
gem 'slack-notifier', '~> 2.3.2'
gem 'pagerduty', '3.0.0'
gem 'gitlab', '~> 4.17'
gem 'git', '>= 1.11.0'
gem 'slack-ruby-client', '~> 0.17.0'
gem 'graphql-client', '~> 0.17.0'
gem 'prawn', '~> 2.4'
gem 'prawn-table', '~> 0.2.2'
gem 'newrelic_rpm', '~> 8.9'
gem 'sidekiq-cron', '~> 1.7.0'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.4'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'rubocop'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
New Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.2.2'
gem 'dotenv-rails'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 7.0', '>= 7.0.4.3'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '>= 5.6.4'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.10'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
gem 'redis', '~> 4.2.5'
gem 'hiredis', '~> 0.6.3'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.7.3', require: false
gem 'sidekiq', '~> 6.4.2'
# Auto throttle jobs
gem 'sidekiq-throttled', '~> 0.15.0'
# For interacting with Github API
gem 'octokit', '~> 4.21.0'
gem 'faraday-http-cache', '~> 2.2.0'
# For docker api
gem 'docker-api', '~> 2.0.0'
# Collect metrics
gem 'dogstatsd-ruby', '~> 4.8.2'
# Asana
gem 'asana', '~> 0.10.2'
gem 'httparty', '~> 0.18.1'
gem 'redlock', '~> 1.2.1'
gem 'slack-notifier', '~> 2.4'
gem 'pagerduty', '3.0.0'
gem 'gitlab', '~> 4.17'
gem 'git', '>= 1.11.0'
gem 'slack-ruby-client', '~> 0.17.0'
gem 'graphql-client', '~> 0.18.0'
gem 'graphql', '~> 2.0', '>= 2.0.22'
gem 'prawn', '~> 2.4'
gem 'prawn-table', '~> 0.2.2'
gem 'newrelic_rpm', '~> 9.2', '>= 9.2.2'
gem 'sidekiq-cron', '~> 1.7.0'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.4'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'rubocop'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "matrix", "~> 0.4.2"
# For Ruby 3.2 YAML behaviour issues - https://stackoverflow.com/a/71192990
gem 'psych', '< 4'
Apart from ruby upgrade, What could be the possible reason behind this behaviour and did someone else observe something similar ?