Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6474675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:36:56+00:00 2026-05-25T06:36:56+00:00

For the curious : It turns out my memory leak had nothing to do

  • 0

For the curious: It turns out my memory leak had nothing to do with what I put in the sample here. I thought I had the issue nailed down to some sample code, but my sample code had different issues. I did end up finding my real issue though, and that’s here: Ruby Symbol#to_proc leaks references in 1.9.2-p180?

I have two ruby classes (Generator and Member, in this example) where Generator serves as a factory (in a loose definition of the term) of Member objects, and each Member holds a reference to the Generator that constructed it.

Code:

class Generator
  def new_member
    Member.new
  end
end

class Member
  attr_reader :generator

  def self.get(generator)
    @generator = generator
    puts "Provided generator: #{generator}"
    generator.new_member
  end
end

Using IRB, I’d expect that if I simply call I simply call Member.get(Generator.new), but not actually assign the result to anything, both the reference to the newly-constructed Generator object and the newly-constructed Member object should have zero references laying around. So the garbage collector should collect both objects. But it only collects the Members, leaving the Generator sitting around:

ruby-1.9.2-p180 :001 > Member.get(Generator.new)
Provided generator: #<Generator:0x007fcf398015c8>
 => #<Member:0x007fcf39801550>
ruby-1.9.2-p180 :006 > GC.start
 => nil 
ruby-1.9.2-p180 :007 > ObjectSpace.each_object(Member){|m| puts m}
 => 0 
ruby-1.9.2-p180 :008 > ObjectSpace.each_object(Generator){|g| puts g}
#<Generator:0x007fcf398015c8>
 => 1

(ObjectSpace.each_object, as I understand it, returns a list of references to a given class still on ruby’s heap.)

Why is there still a reference to the Generator object sitting around? I haven’t saved it to a variable in any way, so there shouldn’t be anything referencing it anymore. The Member object got collected, so its instance variable that references the Generator class shouldn’t be preventing it from getting collected.

I’m not just curious, either. We have a Sinatra app that has a similar class structure, and the equivalent Generator class stores a huge cache of Member objects, several hundred megs per request, and it never gets collected. Ruby runs out of memory and the app server has to restart every dozen or so requests.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:36:57+00:00Added an answer on May 25, 2026 at 6:36 am

    When you call

    Member.get(Generator.new)
    

    you set the class instance variable @generator for the Member class:

    @generator = generator
    

    And by calling the initial line Member.get(Generator.new), there is nothing that would create a Member, you simply create an instance of Generator, which is then assigned to the class instance variable.

    That leaves:

    • One instance of Generator assigned to Member class instance variable @generator
    • No instance of Member was ever created
    • The class instance variable @generator of Member will not get collected because the class Member will not get collected

    -> The results you receive are perfectly normal, nothing wrong with Ruby’s Garbage Collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just curious what is the best practice for solving the following issue. I have
So i bet i got you curious with that title. =) Here's the scenario
I have a kernel does a linear least square fit. It turns out threads
Im currently trying out the ajax feature, and im curious about how can i
Every once in a while a borked Eclipse build gets nailed down to the
Just curious, can flash take each frame and export it out as a image
I am curious what sort of options there are out there for MVC 3
Curious whether folks have setup 2 way transactional replication on the tables ASP.NET uses
Curious, what happens when you return keyword this from a struct in C#? For
Curious what "Toast" means? Saw this and am curious... Similar Posts How to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.