Force_recycle

Hi List,

is there a way to tell the GC that an object isn't needed anymore?

There is a rb_force_recycle which may do the trick, but I found no
way to access it from ruby.

(and of course this shouldn't be necessary in 'most' situations)

cheers

Simon

Hi,

At Wed, 10 Aug 2005 22:56:45 +0900,
Kroeger Simon (ext) wrote in [ruby-talk:151493]:

is there a way to tell the GC that an object isn't needed anymore?

There is a rb_force_recycle which may do the trick, but I found no
way to access it from ruby.

No.

···

--
Nobu Nakada

The cleanest way i can think of would be to set the references to nil,
and call GC.start. This will not in every case collect the object
(because there may be something looking like a reference in some
register).
As an insane idea maybe you could use something like weakref to
channel all pointers through one object if you're program logic may
hold some unused references somewhere. (Though getting rid of the
unused references may make more sense).

E.g.:

$ cat collectme.rb
class Collectme
  def initialize
    @data = Array.new(2**18)
  end
  
  def hello
    puts "I'm big!"
  end
end

class Collected
  class << self
    alias :_new :new
  end

  def self.new
    @@instance ||= _new
  end
end

class CollectProxy
  def initialize(object)
    @_object = object
  end

  def method_missing(m, *args, &block)
    raise "Already collected" if @_object == Collected.new
    @_object.__send__(m, *args, &block)
  end

  def respond_to?(*m)
    raise "Already collected" if @_object == Collected.new
    super(*m) || @_object.respond_to?(*m)
  end

  def methods
    super() + @_object.methods
  end

  def garbage_collect
    @_object = Collected.new
    GC.start
  end
end

puts "Memory before allocation:"
system "ps -o vsize -p #{$$}"

my_big_objects = Array.new(12) { CollectProxy.new(Collectme.new) }

puts "Memory after allocation:"
system "ps -o vsize -p #{$$}"

my_big_objects.each do | my_big_object | my_big_object.hello end

puts "Memory after call:"
system "ps -o vsize -p #{$$}"

my_big_objects.each do | my_big_object | my_big_object.garbage_collect end

puts "Memory after collect:"
system "ps -o vsize -p #{$$}"

puts "Calling collected object"
my_big_objects.each do | my_big_object | my_big_object.hello end

$ ruby collectme.rb
Memory before allocation:
   VSZ
  3084
Memory after allocation:
   VSZ
15420
I'm big!
I'm big!
I'm big!
I'm big!
I'm big!
I'm big!
I'm big!
I'm big!
I'm big!
I'm big!
I'm big!
I'm big!
Memory after call:
   VSZ
15420
Memory after collect:
   VSZ
  4112
Calling collected object
collectme.rb:29:in `method_missing': Already collected (RuntimeError)
        from collectme.rb:68
        from collectme.rb:68:in `each'
        from collectme.rb:68

but this is just for fun.

regards,

Brian

···

On 10/08/05, Kroeger Simon (ext) <simon.kroeger.ext@siemens.com> wrote:

Hi List,

is there a way to tell the GC that an object isn't needed anymore?

There is a rb_force_recycle which may do the trick, but I found no
way to access it from ruby.

(and of course this shouldn't be necessary in 'most' situations)

cheers

Simon

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/