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

  • SEARCH
  • Home
  • 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 7885049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:57:40+00:00 2026-06-03T04:57:40+00:00

When I run this and then watch the memory consumption of my ruby process

  • 0

When I run this and then watch the memory consumption of my ruby process in OSX Activity Monitor, the memory increases at about 3 MB/s.

If I remove the transaction it about halves the memory consumption but still, the memory footprint keeps going up. I have an issue on my production app where Heroku kills the process because of its memory consumption.

Is there a way of doing the below, in a way that won’t increase memory? If I comment out the .save line then it’s okay but of course this isn’t a solution.

ActiveRecord::Base.transaction do
  10000000.times do |time|
    puts "---- #{time} ----"
    a = Activity.new(:name => "#{time} Activity")
    a.save!(:validate => false)
    a = nil
  end
end

I am running this using delayed_job.

  • 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-06-03T04:57:41+00:00Added an answer on June 3, 2026 at 4:57 am

    The a = nil line is unnecessary and you can remove that.

    You’re creating a lot of objects every time you loop – two strings, two hashes, and an Activity object so I’m not surprised you’re experiencing high memory usage, especially as you’re looping 10 million times! There doesn’t appear to be a more memory efficient way to write this code.

    The only way I can think of to reduce memory usage is to manually start the garbage collector every x number of iterations. Chances are Ruby’s GC isn’t being aggressive enough. You don’t, however, want to invoke it every iteration as this will radically slow your code. Maybe you could use every 100 iterations as a starting point and go from there. You’ll have to profile and test what is most effective.

    The documentation for the GC is here.

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

Sidebar

Related Questions

I have run this program before and it worked fine. Then I added the
i am using PhoneGap,n using this code in xCode then run properly and database
If I run Profiler, then it suggests a lot of indexes like this one
I run this piece of code on Visual C++ 2010 char c[10]; cin.get(&c[0],5); cin.get(&c[2],4);
I run this command : select * from LIST where JCODE = 8 and
I run this in localhost and it works. In the server the PHP script
How can I run this on linux command line when my username has an
How do I run this command with subprocess? I tried: proc = subprocess.Popen( '''ECHO
when I run this JS in FF or Safari it works just right but
if I run this under c# from p in Addresses where p.Address2 == null

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.