Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created February 1, 2015 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/b5cbd2627cd6c169684b to your computer and use it in GitHub Desktop.
Save tenderlove/b5cbd2627cd6c169684b to your computer and use it in GitHub Desktop.
# Activate the gem you are reporting the issue against.
gem 'activerecord'
gem 'activesupport'
require 'active_support'
require 'active_support/core_ext/numeric/conversions'
require 'objspace'
require 'active_record'
def Process.memory_usage
(`ps -o rss= -p #{Process.pid}`.chomp.to_i * 1024)
end
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
create_table :coupons, force: true do |t|
t.text :name
end
create_table :sites, force: true do |t|
t.text :name
end
create_table :products, force: true do |t|
t.text :name
end
create_table :coupons_sites, force: true, id: false do |t|
t.references :coupon
t.references :site
end
create_table :coupons_products, force: true, id: false do |t|
t.references :coupon
t.references :product
end
end
class Coupon < ActiveRecord::Base
has_and_belongs_to_many :products
has_and_belongs_to_many :sites
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :coupons
end
class Site < ActiveRecord::Base
has_and_belongs_to_many :coupons
end
puts
puts
puts "Memory usage: #{Process.memory_usage.to_s(:human_size)}"
Coupon.transaction do
#1.upto(250_000) do |i|
5.times do |i|
Coupon.create!
end
GC.start
ObjectSpace.dump_all(output: File.open('heap1.json','w'))
60.times do |i|
Coupon.create!
end
GC.start
ObjectSpace.dump_all(output: File.open('heap2.json','w'))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment