From: Eric Hodel Date: 2007-09-30T08:46:31+09:00 Subject: Re: ObjectSpace reveals order? On Sep 28, 2007, at 18:18 , Todd Benson wrote: > If I do... > > ObjectSpace.each_object { |o| puts o; gets } > > will it give me the consecutive order of the loading of the objects > (i.e. the time order that memory is allocated on the heap)? Well, its kinda backwards*: $ cat test.rb items = ['a', 'b'] ObjectSpace.each_object do |o| p o.object_id => o end $ ruby test.rb | head -3 {1001920=>"b"} {1001930=>"a"} {1001940=>["a", "b"]} [] was allocated first, then 'a', then 'b', not the other way around. * This example was too small to invoke the garbage collector, so it will be in reverse order. With garbage collection you aren't guaranteed any ordering. > Or is it a tree structure underneath or something strange like that? You can infer the structure of ruby's heap if you make enough objects. Replace items = ['a', 'b'] with ('a'..'aaa').to_a and add GC.disable and you'll see something like: {987780=>"aaa"} # <-- end (bottom) of first heap {987800=>"zz"} {987820=>"zy"} [...] {1035860=>Kernel} {1035900=>Class} {1035910=>Module} {1035920=>Object} # <-- start (top) of first heap {1820790=>"Object"} # <-- last used address of second heap {1820800=>"Object"} {1820810=>"1035920"} {1820820=>"{1035920=>Object}"} [...] {1828610=>"1022660"} {1828620=>"{1022660=>\"Errno::EREMOTE\"}"} {1828630=>{1022660=>"Errno::EREMOTE"}} # <-- start (top) of second heap Ruby's heaps look something like: heaps = [ [ ... ], # objects live in here ... # more arrays of objects, as necessary ] This was on OS X. > Why do I get String objects right off the bat like... > > ' does not evaluate to a gem specification You have RUBYOPT=-rubygems