diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-11-30 11:50:11 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-11-30 11:50:11 +0000 |
commit | 68e4442ec720ca6730959681951f5862f97b5772 (patch) | |
tree | 95495e8e705a47ec20880318a0e82e1fb3b640e3 /activerecord/test/cases | |
parent | c5bdf6c5aee61848bee67b307287e2f28ddca173 (diff) | |
download | rails-68e4442ec720ca6730959681951f5862f97b5772.tar.gz rails-68e4442ec720ca6730959681951f5862f97b5772.tar.bz2 rails-68e4442ec720ca6730959681951f5862f97b5772.zip |
Fix memory leak in development mode
Keying these hashes by klass causes reloadable classes to never get
freed. Thanks to @thedarkone for pointing this out in
the comments on 221571beb6b4bb7437989bdefaf421f993ab6002.
This doesn't seem to make a massive difference to performance.
Benchmark
---------
require 'active_record'
require 'benchmark/ips'
class Post < ActiveRecord::Base
establish_connection adapter: 'sqlite3', database: ':memory:'
end
GC.disable
Benchmark.ips(20) do |r|
r.report { Post.connection }
end
Before
------
Calculating -------------------------------------
5632 i/100ms
-------------------------------------------------
218671.0 (±1.9%) i/s - 4364800 in 19.969401s
After
-----
Calculating -------------------------------------
8743 i/100ms
-------------------------------------------------
206525.9 (±17.8%) i/s - 4039266 in 19.992590s
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/connection_adapters/connection_handler_test.rb | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index 2ddabe058f..3e33b30144 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -4,8 +4,8 @@ module ActiveRecord module ConnectionAdapters class ConnectionHandlerTest < ActiveRecord::TestCase def setup - @klass = Class.new(Base) - @subklass = Class.new(@klass) + @klass = Class.new(Base) { def self.name; 'klass'; end } + @subklass = Class.new(@klass) { def self.name; 'subklass'; end } @handler = ConnectionHandler.new @pool = @handler.establish_connection(@klass, Base.connection_pool.spec) @@ -36,13 +36,11 @@ module ActiveRecord end def test_retrieve_connection_pool_uses_superclass_pool_after_subclass_establish_and_remove - @handler.establish_connection 'north america', Base.connection_pool.spec - assert_same @handler.retrieve_connection_pool(@klass), - @handler.retrieve_connection_pool(@subklass) + sub_pool = @handler.establish_connection(@subklass, Base.connection_pool.spec) + assert_same sub_pool, @handler.retrieve_connection_pool(@subklass) @handler.remove_connection @subklass - assert_same @handler.retrieve_connection_pool(@klass), - @handler.retrieve_connection_pool(@subklass) + assert_same @pool, @handler.retrieve_connection_pool(@subklass) end def test_connection_pools |