# Keep track of all instances of a class without losing GC?

**URL:** <https://www.ruby-forum.com/t/keep-track-of-all-instances-of-a-class-without-losing-gc/66595>\
**Category:** Ruby\
**Created:** [August 27, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/keep-track-of-all-instances-of-a-class-without-losing-gc/66595 "2006-08-27T16:45:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jez](https://www.ruby-forum.com/u/jez)\
**Post date:** [August 27, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/keep-track-of-all-instances-of-a-class-without-losing-gc/66595/1 "2006-08-27T16:45:29Z")

</div>

Hi,

I want to be able to keep track of all instances of a class using  
something simple like this:

Class Thing  
@@allthings = []

```
def initialize
    @@allthings.push self
end

```

end

However an obvious drawback to this approach is that instances of this  
class will never be garbage collected due to the reference held in  
@@allthings.

Is there a way to make it so this reference in @@allthings “doesn’t  
count”, so to speak?

Or is there a better way of doing it?

Thanks for your help

---

<div class="post-metadata">

**Author:** ![jez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jez](https://www.ruby-forum.com/u/jez)\
**Post date:** [August 27, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/keep-track-of-all-instances-of-a-class-without-losing-gc/66595/2 "2006-08-27T17:04:43Z")

</div>

On Sun, 27 Aug 2006, Jez S. wrote:

> end
> 
> Thanks for your help

require ‘weakref’

check out the docs.

-a

---

<div class="post-metadata">

**Author:** ![jez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jez](https://www.ruby-forum.com/u/jez)\
**Post date:** [August 27, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/keep-track-of-all-instances-of-a-class-without-losing-gc/66595/3 "2006-08-27T17:00:42Z")

</div>

On Sun, 27 Aug 2006 23:45:34 +0900, Jez S. wrote:

> ```
> end
> 
> ```
> 
> Thanks for your help

Have a look at weakref.rb. Either use that directly, or copy ideas from  
weakref.rb to create your own system.

–Ken B.

---

<div class="post-metadata">

**Author:** ![jez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jez](https://www.ruby-forum.com/u/jez)\
**Post date:** [August 27, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/keep-track-of-all-instances-of-a-class-without-losing-gc/66595/4 "2006-08-27T17:06:43Z")

</div>

On 8/27/06, Jez S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However an obvious drawback to this approach is that instances of this  
> class will never be garbage collected due to the reference held in  
> @@allthings.
> 
> Is there a way to make it so this reference in @@allthings “doesn’t  
> count”, so to speak?
> 
> Or is there a better way of doing it?

ObjectSpace already does it for you:  
ObjectSpace.each\_object(Thing) {|x| print x}

You could also use weak references, but Object Space is  
much easier most of the time.

---

<div class="post-metadata">

**Author:** ![jez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jez](https://www.ruby-forum.com/u/jez)\
**Post date:** [August 27, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/keep-track-of-all-instances-of-a-class-without-losing-gc/66595/5 "2006-08-27T17:58:48Z")

</div>

On 27-aug-2006, at 16:45, Jez S. wrote:

> I want to be able to keep track of all instances of a class using  
> something simple like this:
> 
> Class Thing  
> @@allthings = []
> 
> ```
> def initialize
> @@allthings.push self
> end
> 
> ```
> 
> end

weakref might be of help indeed, you also can use lookups (3 extra  
lines):

class Thing  
@@things = []  
def self.things  
@@things.map{ | id | ObjectSpace.\_id2ref(id) }  
end

```
 def initialize
     @@things << self.object_id
 end

```

end

After that you might get lost references when object get garbage  
collected, but if you don’t need that - what’s the point ? 🙂
