Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save ajsharp/5727071 to your computer and use it in GitHub Desktop.
Run using ruby 1.9.3p392
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
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
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
end
def self.go
new.run
end
end
Test.go
object_report
object_report(This)
full_report
puts "running gc"
GC.start
puts "=" * 60
puts "after gc run"
object_report
object_report(This)
full_report
@ajsharp

ajsharp commented Jun 7, 2013

Copy link
Copy Markdown
Author

Results from https://gist.github.com/ajsharp/5727071/ed3d15672482fca7c1d6b82e2924a3de37389120

number of objects in memory = 253044
number of (This) objects in memory = 100001
 152401  |  String
 100001  |  This
   207  |  Class
   167  |  RubyVM::InstructionSequence
    99  |  Encoding
    43  |  Array
    37  |  RubyVM::Env
    22  |  Regexp
    21  |  Module
    18  |  Proc
running gc
============================================================
after gc run
number of objects in memory = 203687
number of (This) objects in memory = 100001
 103043  |  String
 100001  |  This
   207  |  Class
   167  |  RubyVM::InstructionSequence
    99  |  Encoding
    44  |  Array
    37  |  RubyVM::Env
    22  |  Regexp
    21  |  Module
    18  |  Proc

@ajsharp

ajsharp commented Jun 7, 2013

Copy link
Copy Markdown
Author

Results from https://gist.github.com/ajsharp/5727071/c76f88ea936165e76bd0a3d025d7abcdaba1fcdd

number of objects in memory = 253050
number of (This) objects in memory = 100001
 152406  |  String
 100001  |  This
   207  |  Class
   168  |  RubyVM::InstructionSequence
    99  |  Encoding
    43  |  Array
    37  |  RubyVM::Env
    22  |  Regexp
    21  |  Module
    18  |  Proc
running gc
============================================================
after gc run
number of objects in memory = 103688
number of (This) objects in memory = 0
 103044  |  String
   207  |  Class
   168  |  RubyVM::InstructionSequence
    99  |  Encoding
    44  |  Array
    37  |  RubyVM::Env
    22  |  Regexp
    21  |  Module
    18  |  Proc
     7  |  Float

@PriteshJain

Copy link
Copy Markdown

I tried it against ruby 2.0.0 patch195
got the following results

for https://gist.github.com/ajsharp/5727071/c76f88ea936165e76bd0a3d025d7abcdaba1fcdd

number of objects in memory = 240971
number of (This) objects in memory = 100001
139748 | String
100001 | This
462 | RubyVM::InstructionSequence
256 | Class
226 | Array
100 | Encoding
63 | Regexp
24 | Hash
21 | Module
18 | Gem::Requirement
running gc

============================================================

after gc run
number of objects in memory = 106086
number of (This) objects in memory = 0
104863 | String
462 | RubyVM::InstructionSequence
256 | Class
226 | Array
100 | Encoding
63 | Regexp
24 | Hash
21 | Module
18 | Gem::Requirement
10 | Gem::Version

for https://gist.github.com/ajsharp/5727071/ed3d15672482fca7c1d6b82e2924a3de37389120

number of objects in memory = 259711
number of (This) objects in memory = 100001
158489 | String
100001 | This
461 | RubyVM::InstructionSequence
256 | Class
226 | Array
100 | Encoding
63 | Regexp
24 | Hash
21 | Module
18 | Gem::Requirement
running gc

============================================================

after gc run
number of objects in memory = 106084
number of (This) objects in memory = 0
104862 | String
461 | RubyVM::InstructionSequence
256 | Class
226 | Array
100 | Encoding
63 | Regexp
24 | Hash
21 | Module
18 | Gem::Requirement
10 | Gem::Version

(This) objects are properly garbage collected in both.

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