Skip to content

Instantly share code, notes, and snippets.

@ajsharp
Last active December 18, 2015 04:29
Show Gist options
  • Select an option

  • Save ajsharp/5725706 to your computer and use it in GitHub Desktop.

Select an option

Save ajsharp/5725706 to your computer and use it in GitHub Desktop.
def object_report(type = nil)
if type
puts "number of (#{type}) objects in memory = #{ObjectSpace.each_object(type).count}"
else
puts "number of objects in memory = #{ObjectSpace.each_object.count}"
end
end
def full_report
# report
# ObjectSpace.each_object
ObjectSpace.each_object.to_a.inject(Hash.new 0) do |h,o|
h["#{o.class}"] += 1; h
end.sort_by{ |k,v| -v }.first(10)
.each{ |k,v| printf "% 6d | %s\n", v, k }
end
puts "before loading bldr"
object_report
full_report
require File.expand_path(File.dirname(__FILE__) + '/../lib/bldr')
puts "after loading bldr"
object_report
full_report
puts
puts "running GC before creating Bldr::Node's"
GC.start
class This
def doit
100000.times { |i| instance_variable_set("@this#{i}", This.new) }
end
end
class Test
def initialize
@this = This.new
end
def run
@this.doit
puts "# of ivars=#{@this.instance_variables.size}"
puts "creating 1000 Bldr::Node objects"
# node = Bldr::Node.new(parent: @this) { attribute :foo, 'bar' }
# puts "node ivars=#{node.instance_variables}"
end
def self.go
new.run
end
end
Test.go
object_report
object_report(Bldr::Node)
object_report(This)
full_report
puts "running gc"
GC.start
puts "=" * 60
puts "after gc run"
object_report
object_report(Bldr::Node)
object_report(This)
full_report
@ajsharp

ajsharp commented Jun 6, 2013

Copy link
Copy Markdown
Author

Results:

before loading bldr
number of objects in memory = 6257
  5139  |  String
   339  |  Array
   205  |  RubyVM::InstructionSequence
   205  |  Class
   108  |  MatchData
    99  |  Encoding
    37  |  RubyVM::Env
    35  |  Proc
    24  |  Regexp
    21  |  Module
after loading bldr
number of objects in memory = 83726
 52646  |  String
 17101  |  Array
  4121  |  Gem::Version
  3986  |  Gem::Requirement
  2752  |  Gem::Dependency
   874  |  RubyVM::InstructionSequence
   617  |  Time
   616  |  Gem::Specification
   356  |  MatchData
   247  |  Class

running GC before creating Bldr::Node's
creating 1000 Bldr::Node objects
number of objects in memory = 100350
number of (Bldr::Node) objects in memory = 0
number of (This) objects in memory = 10000
 61755  |  String
 15148  |  Array
 10000  |  This
  4121  |  Gem::Version
  3986  |  Gem::Requirement
  2752  |  Gem::Dependency
   770  |  RubyVM::InstructionSequence
   617  |  Time
   616  |  Gem::Specification
   248  |  Class
running gc
after gc run
number of objects in memory = 80350
number of (Bldr::Node) objects in memory = 0
number of (This) objects in memory = 10000
 41755  |  String
 15148  |  Array
 10000  |  This
  4121  |  Gem::Version
  3986  |  Gem::Requirement
  2752  |  Gem::Dependency
   770  |  RubyVM::InstructionSequence
   617  |  Time
   616  |  Gem::Specification
   248  |  Class

@anveo

anveo commented Jun 6, 2013

Copy link
Copy Markdown
before loading bldr
number of objects in memory = 6295
  5149  |  String
   337  |  Array
   237  |  Class
   205  |  RubyVM::InstructionSequence
   106  |  MatchData
    99  |  Encoding
    37  |  RubyVM::Env
    35  |  Proc
    24  |  Regexp
    21  |  Module
after loading bldr
number of objects in memory = 7387
  5716  |  String
   551  |  RubyVM::InstructionSequence
   433  |  Array
   247  |  Class
    99  |  Encoding
    73  |  Regexp
    58  |  RubyVM::Env
    38  |  Proc
    34  |  Gem::Requirement
    30  |  Gem::Version

running GC before creating Bldr::Node's
creating 1000 Bldr::Node objects
number of objects in memory = 26333
number of (Bldr::Node) objects in memory = 0
number of (This) objects in memory = 10000
 14840  |  String
 10000  |  This
   528  |  RubyVM::InstructionSequence
   301  |  Array
   248  |  Class
    99  |  Encoding
    73  |  Regexp
    58  |  RubyVM::Env
    36  |  Proc
    34  |  Gem::Requirement
running gc
after gc run
number of objects in memory = 26142
number of (Bldr::Node) objects in memory = 0
number of (This) objects in memory = 10000
 14612  |  String
 10000  |  This
   528  |  RubyVM::InstructionSequence
   338  |  Array
   248  |  Class
    99  |  Encoding
    73  |  Regexp
    58  |  RubyVM::Env
    36  |  Proc
    34  |  Gem::Requirement

@ajsharp

ajsharp commented Jun 6, 2013

Copy link
Copy Markdown
Author

Now they get freed:

before loading bldr
number of objects in memory = 6264
  5144  |  String
   340  |  Array
   206  |  RubyVM::InstructionSequence
   205  |  Class
   108  |  MatchData
    99  |  Encoding
    37  |  RubyVM::Env
    35  |  Proc
    24  |  Regexp
    21  |  Module
after loading bldr
number of objects in memory = 83729
 52648  |  String
 17101  |  Array
  4121  |  Gem::Version
  3986  |  Gem::Requirement
  2752  |  Gem::Dependency
   875  |  RubyVM::InstructionSequence
   617  |  Time
   616  |  Gem::Specification
   356  |  MatchData
   247  |  Class

running GC before creating Bldr::Node's
creating 1000 Bldr::Node objects
number of objects in memory = 100354
number of (Bldr::Node) objects in memory = 0
number of (This) objects in memory = 10001
 61757  |  String
 15148  |  Array
 10001  |  This
  4121  |  Gem::Version
  3986  |  Gem::Requirement
  2752  |  Gem::Dependency
   771  |  RubyVM::InstructionSequence
   617  |  Time
   616  |  Gem::Specification
   248  |  Class
running gc
after gc run
number of objects in memory = 70353
number of (Bldr::Node) objects in memory = 0
number of (This) objects in memory = 0
 41757  |  String
 15148  |  Array
  4121  |  Gem::Version
  3986  |  Gem::Requirement
  2752  |  Gem::Dependency
   771  |  RubyVM::InstructionSequence
   617  |  Time
   616  |  Gem::Specification
   248  |  Class
    99  |  Encoding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment