Tag: Ruby 1.8.7

Ruby, Mongoid and (again) memory leaks – next Identity map problem

Lately I've fixed an issue with Mongoid driver (read more about this). Memory consumption stabilized at a relatively low level until... gem update mongo. Below you can see Munin memory and CPU consumption charts from a frontend server (frontend only presents data):

There were some heavy issues on october 23d. Server went really crazy. Behavior was similar to one mentioned in previous post about Mongoid. But IdentityMap has been turned off so WTF? Well I don't know what happend, but what do I know is how to fix it. Edit your Padrino/Sinatra app file (app/app.rb) and place this in your project class:

after do
  Mongoid::IdentityMap.clear
end

This will clear IdentityMap after each request.

I know, that this is not the best solution, however it is good enough for me - and what's more important - it works fine.

Handling large seed files in Ruby on Rails

Sometimes seed files can get messy and big. It can be real pain it the ass to manage them. Here is fast way to split single seeds.rb file:

  1. Create directory called seeds in your db/ directory (mkdir ./db/seeds)
  2. Remove all stuff from seeds.rb and move it into your newly created files under db/seeds/ directory (put them accordingly to your own app logic)
  3. Paste code presented below into seeds.rb file
  4. Run rake db:seed

Seeds.rb file source code:

# coding: utf-8

%w{
  filename1 filename2 filename3...filenameN
}.each do |part|
  require File.expand_path(File.dirname(__FILE__))+"/seeds/#{part}.rb"
end

Why haven't I use auto-include and instead I've listed all the files? Well I wanted to maintain my seed parts load order so those parts will be loaded accordingly to my order (not based on file names).

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑