Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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