# ObjectSpace reveals order?

**URL:** <https://www.ruby-forum.com/t/objectspace-reveals-order/115846>\
**Category:** Ruby\
**Created:** [September 29, 2007, 3:19am UTC](https://www.ruby-forum.com/t/objectspace-reveals-order/115846 "2007-09-29T03:19:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [September 29, 2007, 3:19am UTC](https://www.ruby-forum.com/t/objectspace-reveals-order/115846/1 "2007-09-29T03:19:20Z")

</div>

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)? Or is it  
a tree structure underneath or something strange like that?

Why do I get String objects right off the bat like…

’ does not evaluate to a gem specification

This appears to be source that the interpreter ran and outputted.  
I see it on a WindowsXP machine using the 1.8.6 one-click installer.

Todd

P.S.

I understand that this question is hard to answer exactly because you  
have to create objects to read the objects you have.

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [September 29, 2007, 5:33am UTC](https://www.ruby-forum.com/t/objectspace-reveals-order/115846/2 "2007-09-29T05:33:11Z")

</div>

On 9/28/07, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ’ does not evaluate to a gem specification
> 
> This appears to be source that the interpreter ran and outputted.  
> I see it on a WindowsXP machine using the 1.8.6 one-click installer.

ObjectSpace has everything. Including the String objects that are  
sitting around in memory, waiting for you to (in this case) someday  
try to load a faulty RubyGem.

It’s true that ascending object ids indicate creation order, but I  
doubt it would be a good idea to rely on that. Not every Ruby  
implementation handles them the same way. object\_ids are meant to be  
unique, and nothing else.

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [September 29, 2007, 6:03am UTC](https://www.ruby-forum.com/t/objectspace-reveals-order/115846/3 "2007-09-29T06:03:00Z")

</div>

On 9/28/07, Wilson B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> It’s true that ascending object ids indicate creation order, but I  
> doubt it would be a good idea to rely on that. Not every Ruby  
> implementation handles them the same way. object\_ids are meant to be  
> unique, and nothing else.

Thanks for the answer. I think I understand a tiny bit how the Ruby  
memory is allocated. I was just wondering if it was a question of  
when. But, as you say, every implementation may be different. The  
question arose out of a couple of things out of other threads. I  
started thinking about code obfuscation, and also whether you can –  
within Ruby – hold on to all interpreted source if need be (while  
recalling some other threads on the topic). Then I started thinking  
about things like, if you do a count (within the process), you’ll have  
to allocate memory just to see what’s going on in an already volatile  
environment. I suppose I’ll have to read up on GC stuff.

I’m personally not a big fan of obfuscation, but I’m starting to see  
how it could be accomplished (legally) without hurting anyone’s  
feelings.

the jury is still out for me on ethicality

Todd

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [September 30, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/objectspace-reveals-order/115846/4 "2007-09-30T23:52:59Z")

</div>

On 9/29/07, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> {1001940=\>[“a”, “b”]}
> 
> [] was allocated first, then ‘a’, then ‘b’, not the other way around.

Ok, logistically this makes sense. I see.

> - 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

OK. Got ya.

> > ’ does not evaluate to a gem specification
> 
> You have RUBYOPT=-rubygems

Thanks!

Todd

---

<div class="post-metadata">

**Author:** ![toadkicker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5fc32e/32.png) [@toadkicker](https://www.ruby-forum.com/u/toadkicker)\
**Post date:** [September 30, 2007, 1:47am UTC](https://www.ruby-forum.com/t/objectspace-reveals-order/115846/5 "2007-09-30T01:47:54Z")

</div>

On Sep 28, 2007, at 18:18 , Todd B. 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
