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

J

Jez Stephens

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
 
K

Ken Bloom

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

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

--Ken Bloom
 
A

ara.t.howard

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

require 'weakref'

check out the docs.

-a
 
T

Tomasz Wegrzanowski

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?

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

Julian 'Julik' Tarkhanov

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 ? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top