[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ObjectSpace.define_finalizer and memory leaks

ara.t.howard

9/25/2007 4:02:00 PM


i don't think i've ever noticed this behaviour:

cfp:~ > cat a.rb

class C
def self.count
c = 0
ObjectSpace.each_object do |object|
c += 1 if self === object rescue next
end
c
end
end

loop do
c = nil ### try with and without this!!!!!!!!!!!!!!

(2 ** 16).times do
c = C.new
object_id = c.object_id
ObjectSpace.define_finalizer(c){ :nothing }
end

puts "before: #{ C.count }"

GC.start

puts "after: #{ C.count }"
puts
end



run both ways. notice that, without the prior declaration of c, the
code leaks like crazy: the finalizer itself holds a reference to the
object and prevents it being reaped. i don't think i've ever noticed
this behavior before. i understand it - but can this be correct? it
seems like you should be able to define a finalizer on any object
without preventing it from being gc'd!

cheers.

a @ http://draw...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




6 Answers

Ilmari Heikkinen

9/25/2007 4:12:00 PM

0

On 9/25/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> i don't think i've ever noticed this behaviour:
>
> cfp:~ > cat a.rb
>
> class C
> def self.count
> c = 0
> ObjectSpace.each_object do |object|
> c += 1 if self === object rescue next
> end
> c
> end
> end
>
> loop do
> c = nil ### try with and without this!!!!!!!!!!!!!!
>
> (2 ** 16).times do
> c = C.new
> object_id = c.object_id
> ObjectSpace.define_finalizer(c){ :nothing }
> end
>
> puts "before: #{ C.count }"
>
> GC.start
>
> puts "after: #{ C.count }"
> puts
> end
>
>
>
> run both ways. notice that, without the prior declaration of c, the
> code leaks like crazy: the finalizer itself holds a reference to the
> object and prevents it being reaped. i don't think i've ever noticed
> this behavior before. i understand it - but can this be correct? it
> seems like you should be able to define a finalizer on any object
> without preventing it from being gc'd!

The usual way to do finalizers is

class C
def self.finalize(resource)
lambda{ resource.free }
end

def initialize
@resource = Resource.new
ObjectSpace.define_finalizer(self, C.finalize(resource))
end
end

You can't GC the finalizer proc before running it, and you can't
run it if there's a reference to the finalized object somewhere.
Including the finalizer proc.

Sylvain Joyeux

9/25/2007 4:15:00 PM

0

> it seems like you should be able to define a finalizer on any object
> without preventing it from being gc'd!
The only way I am aware of is to use methods ...
ObjectSpace.define_finalizer(obj, &method(:my_finalizer))
--
Sylvain Joyeux http://www.laas.f...

ara.t.howard

9/25/2007 4:33:00 PM

0


On Sep 25, 2007, at 10:14 AM, Sylvain Joyeux wrote:

>> it seems like you should be able to define a finalizer on any object
>> without preventing it from being gc'd!
> The only way I am aware of is to use methods ...
> ObjectSpace.define_finalizer(obj, &method(:my_finalizer))
> --
> Sylvain Joyeux http://ww...
> ~sjoyeux

yeah - trying to prevent the closure... smart. i'm not having luck
though:

cfp:~ > cat a.rb
class C
def self.count
c = 0
ObjectSpace.each_object do |object|
c += 1 if self === object rescue next
end
c
end
end

def nothing
end

loop do
#c = nil ### try with and without this!!!!!!!!!!!!!!

(2 ** 16).times do
c = C.new
ObjectSpace.define_finalizer c, &method(:nothing)
end

puts "before: #{ C.count }"

GC.start

puts "after: #{ C.count }"
puts
end


again, if you un-comment the 'c = nil' line it'll work, but avoid the
closure alone doesn't work on my platform. does it work for you?
did i misunderstand your comment perhaps?

thanks alot for the input.

kind regards.

a @ http://draw...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




ara.t.howard

9/25/2007 4:50:00 PM

0


On Sep 25, 2007, at 10:14 AM, Sylvain Joyeux wrote:

>> it seems like you should be able to define a finalizer on any object
>> without preventing it from being gc'd!
> The only way I am aware of is to use methods ...
> ObjectSpace.define_finalizer(obj, &method(:my_finalizer))
> --
> Sylvain Joyeux http://ww...
> ~sjoyeux

working off of you idea of avoiding any new references whatsoever i
came up with this, which is working for me, can others confirm?

cfp:~ > cat a.rb
class C
def self.count
c = 0
ObjectSpace.each_object do |object|
c += 1 if self === object rescue next
end
c
end
end

def finalizer_for object_id
lambda { p [:finalized, object_id] if $DEBUG }
end

loop do
(2 ** 16).times do
c = C.new
finalizer = finalizer_for c.object_id
c.instance_eval do
ObjectSpace.define_finalizer self, &finalizer
end
end

puts "before: #{ C.count }"

GC.start

puts "after: #{ C.count }"
puts
end


thanks for the inspiration!

kind regards.

a @ http://draw...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Sylvain Joyeux

9/25/2007 5:21:00 PM

0

Well, I thought I already tried and that it worked fine... But I think I found
the problem ...

define_finalizer does not take any block ! (so, it is normal that it is not
called ;-)) You have to write either the thing Ilmari suggested or remove
the '&' before the method (it works, I just tried it)
--
Sylvain

W.T.S.

2/22/2011 3:34:00 PM

0

In article <3ca7m6tgedhlhkn2v6ke584ftmaa66233k@4ax.com>, duckgumbo32
@cox.net says...
>
> On Mon, 21 Feb 2011 15:49:57 -0600, "W.T.S." <m14m@earthlink.net> wrote:
>
> >> >> The absolutely greatest charity giver in the world is the Roman
> >> >> Catholic Church.
>
> >> > After the Church drains billions from third world
> >> >countries it presumes to be "charitable" in giving some
> >> >back - along with efforts to prevent the birth control
> >> >that said countries desperately need. How kind of them.
>
> >> I gather you're just saying that without support.
>
> >No, it's a fact, the Catholic Church rips off third world countries
> >right and left, collects billions in charity money, most of which goes
> >to the Vatican, and then promotes policies in the third world which are
> >disasters. The Church is a regular predator on the third world.
>
> Thank God for the help that the RCC gives the needy of the world.
The RCC gives essentially nothing to the third world. It only consumes,
and sickens.
>
> The pukester, American-Liar.
> *****
> "The Mass is the most perfect form of lying."
> Pope Paul VI
> *****
--
http://www.rhrealitycheck.org/p...