force_recycle

  • Thread starter Kroeger Simon (ext)
  • Start date
K

Kroeger Simon (ext)

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
 
N

nobu.nokada

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

Brian Schröder

Hi List,
=20
is there a way to tell the GC that an object isn't needed anymore?
=20
There is a rb_force_recycle which may do the trick, but I found no
way to access it from ruby.
=20
(and of course this shouldn't be necessary in 'most' situations)
=20
cheers
=20
Simon
=20
=20

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 =3D Array.new(2**18)
end
=20
def hello
puts "I'm big!"
end
end

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

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

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

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

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

def methods
super() + @_object.methods
end

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


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

my_big_objects =3D 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=20
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


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

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

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top