# How are the object\_ids generated/assigned to objects?

**URL:** <https://www.ruby-forum.com/t/how-are-the-object-ids-generated-assigned-to-objects/66453>\
**Category:** Ruby\
**Created:** [August 25, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/how-are-the-object-ids-generated-assigned-to-objects/66453 "2006-08-25T17:57:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [August 25, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/how-are-the-object-ids-generated-assigned-to-objects/66453/1 "2006-08-25T17:57:19Z")

</div>

Hello,

assume some multithreaded application, thread1 starts and finishes, then  
thread2 starts — how is the possibility that these two threads will  
have the same object\_id? On what it depends?

Thanks,

P.

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [August 25, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/how-are-the-object-ids-generated-assigned-to-objects/66453/2 "2006-08-25T18:05:20Z")

</div>

Pavel S. wrote:

> Hello,
> 
> assume some multithreaded application, thread1 starts and finishes, then  
> thread2 starts — how is the possibility that these two threads will  
> have the same object\_id? On what it depends?  
> If the first thread has stopped and been garbage collected, its  
> object\_id will be available for reuse, and it is perfectly possible for  
> another thread to get it assigned on creation.

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [August 25, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/how-are-the-object-ids-generated-assigned-to-objects/66453/3 "2006-08-25T18:09:20Z")

</div>

On Sat, 26 Aug 2006, Pavel S. wrote:

> Hello,
> 
> assume some multithreaded application, thread1 starts and finishes, then  
> thread2 starts — how is the possibility that these two threads will have  
> the same object\_id? On what it depends?
> 
> Thanks,
> 
> P.

interesting question!

harp:~ \> cat a.rb  
h = {}  
loop{ tid = Thread.new{}.object\_id; (warn h.size.to\_s and  
h.delete(tid)) if h[tid]; h[tid] = true }

harp:~ \> ruby a.rb 2\>&1|head  
4344  
4344  
5428  
5428  
5428  
5428  
6510  
6510  
6510  
6510

looks like they recycle every few thousand threads - depending on how  
long the  
threads run of course.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [August 27, 2006, 5:48am UTC](https://www.ruby-forum.com/t/how-are-the-object-ids-generated-assigned-to-objects/66453/4 "2006-08-27T05:48:58Z")

</div>

Treat it as undefined. No two objects live at the same time will have  
the same object id. That is the only guarantee.
