Tag: Mongo

Ruby, Mongoid and memory leaks – Identity map problem

Where is my memory?

Recently, when browsing large dataset from MongoDB using Padrino and Thin, Ruby started to have memory leaks. After each request it grew approximately 2-5 MB.

I've started debugging by putting following line in my action, to see memory usage increase per request:

puts 'RAM USAGE: ' + `pmap #{Process.pid} | tail -1`[10,40].strip

Results:

RAM USAGE: 796156K
RAM USAGE: 798284K
RAM USAGE: 798824K
RAM USAGE: 799088K
RAM USAGE: 799900K
RAM USAGE: 799900K
RAM USAGE: 812044K
RAM USAGE: 816152K
RAM USAGE: 816292K
RAM USAGE: 816836K
RAM USAGE: 818956K
RAM USAGE: 819088K
RAM USAGE: 830572K
RAM USAGE: 884604K
RAM USAGE: 887648K
RAM USAGE: 892800K
RAM USAGE: 897160K
RAM USAGE: 906960K

As you can see it grows rapidly. When looking at htop things get even worse:

88,4 MB
93,9 MB
97,5 MB
99,2 MB
109,4 MB
113,4 MB
122,7 MB
127,1 MB
...
1,2 GB!

It was definitely too much! Memory consumption reached it's limits and everything slowed down.

I knew that it had something to do with this line:

@analyses = Analysis.finished.page(params[:page] ||= 1).per(10)

Kaminari?

At the beginning I've suspected Kaminari and its pagination engine, however it is just a more complex layer covering some scopes. To check this I've removed Kaminari:

@analyses = Analysis.finished.skip(((params[:page] ||= 1)-1)*10).limit(10)

Unfortunately nothing good happened and memory consumption kept growing with same speed. Interesting is that, when I've turned off all MongoDB indexes:

db.collection1.dropIndexes()
db.collection2.dropIndexes()
...
db.collectionN.dropIndexes()

memory usage grew much slower than before. So WTF?

Identity map!

Finally I've discovered damn source of my problem. It was identity map in Mongoid. What is identity map?

The identity map in Mongoid is a current aid to assist with excessive database queries in relations, and is necessary for eager loading to work. (...) When a document is now loaded from the database, is is automatically added to the identity map by it's class and id. Subsequent request for that document by it's id will not hit the database, but rather pull the document back from the identity map itself. It's primary function in this capacity is to aid in cutting down queries for belongs_to relations when iterating over the parents.

Seems like identity map was never cleared (or it has a memory leak bug in it). Adding:

use Rack::Mongoid::Middleware::IdentityMap

didn't help at all so I've just turned identity map off:

mongo.identity_map_enabled = false

and everything went back to normal. Interesting thing is that identity map in ActiveRecord is by default turned off in Rails because it's known to cause similar problems.

Mongoid, Kaminari i Padrino (Sinatra)

Część z Was już pewnie zauważyła, że w nowej wersji Mongoida (2.0.2) zrezygnowano z will_paginate. Will_paginate okazał się niezbyt efektywny, ponieważ nie obsługiwał opcji limit i kilku innych rzeczy, w skutek czego paginacja dużych zbiorów stawała się bardzo powolna. Zamiast tego jako podstawową metodę paginacji dla nowych wersji Mongoida zaleca się Kaminari. O Kaminari już parokrotnie wspominałem. Jest świetne. Niestety - nie jest aż tak wesoło kiedy trzeba je połączyć z aplikacją która nie jest napisana w Railsach. Zwykłe:

gem 'kaminari'
# i ew. gdzieś tam w kodzie jakby kogoś naszło
require 'kaminari'

na niewiele się zda.

Brak Kaminari skutkuje tego typu błędami:

NoMethodError: undefined method `page' for Note:Class
	/home/mencio/Rails/app/webui/app/controllers/notes.rb:7:in `GET /notes'
	/home/mencio/.rvm/gems/ruby-1.8.7-p299@app/gems/padrino-core-0.9.19/lib/padrino-core/application/routing.rb:340:in `call'
	/home/mencio/.rvm/gems/ruby-1.8.7-p299@app/gems/padrino-core-0.9.19/lib/padrino-core/application/routing.rb:340:in `route'
	/home/mencio/.rvm/gems/ruby-1.8.7-p299@app/gems/sinatra-1.1.3/lib/sinatra/base.rb:649:in `instance_eval'
	/home/mencio/.rvm/gems/ruby-1.8.7-p299@app/gems/sinatra-1.1.3/lib/sinatra/base.rb:649:in `route_eval'
	/home/mencio/.rvm/gems/ruby-1.8.7-p299@app/gems/padrino-core-0.9.19/lib/padrino-core/application/routing.rb:605:in `route!'
	/home/mencio/.rvm/gems/ruby-1.8.7-p299@app/gems/padrino-core-0.9.19/lib/padrino-core/application/routing.rb:605:in `catch'
	/home/mencio/.rvm/gems/ruby-1.8.7-p299@app/gems/padrino-core-0.9.19/lib/padrino-core/application/routing.rb:605:in `route!'
	/home/mencio/.rvm/gems/ruby-1.8.7-p299@app/gems/padrino-core-0.9.19/lib/padrino-core/application/routing.rb:596:in `catch'
	/home/mencio/.rvm/gems/ruby-1.8.7-p299@app/gems/padrino-core-0.9.19/lib/padrino-core/application/routing.rb:596:in `route!'
        # cd ...

Kaminari "z pudełka" średnio współpracuje z (całkiem fajną) mieszanką Padrino (frameworku zbudowanego na Sinatrze) oraz Mongoida. Rozwiązanie jest jednak bajecznie proste (chociaż niezbyt eleganckie). Tworzymy sobie plik config/preload/kaminari.rb a w nim umieszczamy poniższy kod:

require 'kaminari/railtie'
require 'kaminari/engine'
require 'kaminari/config'
require 'kaminari/helpers/action_view_extension'
require 'kaminari/helpers/paginator'
require 'kaminari/models/page_scope_methods'
require 'kaminari/models/configuration_methods'
require 'kaminari/models/mongoid_extension'
::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
::Mongoid::Criteria.send :include, Kaminari::MongoidExtension::Criteria

Następnie w pliku config/boot.rb umieszczamy (w sekcji Padrino.before_load):

  require File.expand_path("config/preload/kaminari", Padrino.root)

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑