# Object IDs

**URL:** <https://www.ruby-forum.com/t/object-ids/97936>\
**Category:** Ruby\
**Created:** [July 11, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/object-ids/97936 "2006-07-11T16:13:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 11, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/object-ids/97936/1 "2006-07-11T16:13:37Z")

</div>

A couple of quick questions:

a) Are object id’s ever reused within the life of a Ruby process?

b) Are object id’s unique across the whole Ruby process, or only within  
a given context?

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 11, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/object-ids/97936/2 "2006-07-11T16:26:23Z")

</div>

Hi,

In message “Re: Object IDs”  
on Tue, 11 Jul 2006 23:10:15 +0900, Alex Y.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|a) Are object id’s ever reused within the life of a Ruby process?

They are recycled by garbage collector.

|b) Are object id’s unique across the whole Ruby process, or only within  
|a given context?

They are unique in the certain point of the execution.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 11, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/object-ids/97936/3 "2006-07-11T16:33:15Z")

</div>

Le 11 juil. 06, à 10:10, Alex Y. a écrit :

> A couple of quick questions:
> 
> a) Are object id’s ever reused within the life of a Ruby process?

I believe that Object#equal? compares ids if not overridden.

> b) Are object id’s unique across the whole Ruby process, or only  
> within a given context?

Unique across the process.

Guillaume.

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 11, 2006, 4:43pm UTC](https://www.ruby-forum.com/t/object-ids/97936/4 "2006-07-11T16:43:15Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: Object IDs”  
> on Tue, 11 Jul 2006 23:10:15 +0900, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> |a) Are object id’s ever reused within the life of a Ruby process?
> 
> They are recycled by garbage collector.  
> Thanks… That explains a minor oddness I’m seeing. Are they recycled  
> before the finalizers are called?

> |b) Are object id’s unique across the whole Ruby process, or only within  
> |a given context?
> 
> They are unique in the certain point of the execution.  
> I thought they would be, just wanted to be sure 🙂

Thanks,

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 12, 2006, 2:27am UTC](https://www.ruby-forum.com/t/object-ids/97936/5 "2006-07-12T02:27:01Z")

</div>

Hi,

In message “Re: Object IDs”  
on Tue, 11 Jul 2006 23:41:11 +0900, Alex Y.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Thanks… That explains a minor oddness I’m seeing. Are they recycled  
|before the finalizers are called?

No. Finalizers are called before the recycling.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 11, 2006, 4:43pm UTC](https://www.ruby-forum.com/t/object-ids/97936/6 "2006-07-11T16:43:27Z")

</div>

On Tue, 11 Jul 2006, Guillaume M. wrote:

> > b) Are object id’s unique across the whole Ruby process, or only within a  
> > given context?
> 
> Unique across the process.

if you wait long enough they can be recycled so i wouldn’t say they are  
unique  
‘across’ a process. that is to say ruby code which does this

hash = Hash.new{|h, object\_id| h[object\_id] \<\< 42}

will stack info about two different objects if it runs long enough or is  
simply unlucky and only runs for a short time.

in otherwords

id = object.object\_id

# time

value = ObjectSpace::\_id2ref id

may or may not retreive the same object and the error may or may not go  
unnoticed.

in summary, i’d say that object\_ids are unique in a given context, that  
context being an instant of time in a given process. as time  
progresses, even  
by a millisecond, the chances of objects being recycled increases. fyi.

regards.

-a

---

<div class="post-metadata">

**Author:** ![regularfry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@regularfry](https://www.ruby-forum.com/u/regularfry)\
**Post date:** [July 12, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/object-ids/97936/7 "2006-07-12T16:20:05Z")

</div>

Yukihiro M. wrote:

> ```
> matz.
> 
> ```

Great. Thanks for confirming that.
