DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat

Ruby Is Dead! - You Need to Take Care of Its Memory Issues

In this post, we take a look at how to address common memory usage issues that can occur in Ruby. Read on to find out more!

Moshe Kaplan user avatar by
Moshe Kaplan
·
Sep. 07, 16 · Tutorial
Like (3)
Save
Tweet
Share
4.28K Views

Join the DZone community and get the full member experience.

Join For Free

One of the common problems with Ruby is its memory usage, just like JVM-based languages (yes Java, I'm talking about you). Actually, Ruby just like Java is based on garbage collector. This GC, if not used correctly, can cause "stop the world scenarios" (as we can see in the attached diagram). In these cases, the GC actually stops responding while consuming the server's whole CPU and in some cases even causes a server reboot.

Image title

Unicorn and Ruby memory leak causes server downtime.

What Can Be Done?

Reboot Your Ruby Periodically

If you are using the popular Unicorn web server, memory issues may be even more severe, as Unicorn forks. During forking, it's copying the whole memory footprint of the parent (Copy on Write or CoW). Therefore,  you may use the " Unicorn Worker Killer " gem that will monitor your server and gracefully will restart specific worker when memory reaches a new high or specific number of requests were served. Since the gem supports randomization, it assures in a very high probability that the server will continue server.

Installing the killer:

gem 'unicorn-worker-killer'

Add to config.ru above "require ::File.expand_path('../config/environment', __FILE__)"

# Unicorn self-process killer
require 'unicorn/worker_killer'

And decide your worker graceful reboot method: 

# Max requests per worker
use Unicorn::WorkerKiller::MaxRequests, 3072, 4096
# Max memory size (RSS) per worker
use Unicorn::WorkerKiller::Oom, (192*(1024**2)), (256*(1024**2))

Tune Your Memory

If you are using Ruby 2.X, you can exploit the CoW by better configuring Unicorn.

config/unicorn.rb (and a detailed explanation on setting at sirupsen

  •  - worker_processes: 1x your cores
  •  - timeout: worker request timeout, should be 15 to 30 sec max
  •  - preload_app: enables CoW, but requires managing connect/disconnect on fork

Take Care of GC Configuration

You can take a look at a detailed discussion at collective idea's post.

The Bottom Line

Dynamic languages have their downs, yet w/ the right design you can keep them up and running.

Keep Performing! -Moshe Kaplan

Memory (storage engine)

Published at DZone with permission of Moshe Kaplan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Deploying Java Serverless Functions as AWS Lambda
  • What Is a Kubernetes CI/CD Pipeline?
  • Bye Bye, Regular Dev [Comic]
  • Better Performance and Security by Monitoring Logs, Metrics, and More

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: