[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how and when a ruby object is destroyed????

sayoyo Sayoyo

1/13/2006 8:22:00 PM

Hi,

I wonder how and when a ruby object is destroyed? Where is the garbage
collector? how it work?
If I set a object to nil, does it means the GC will pick it up or ....?

Can I set a Class to nil? then the GC "destroys" a class ???

Thanks you very much

Sayoyo

4 Answers

Gene Tani

1/13/2006 8:44:00 PM

0


say...@yahoo.com wrote:
> Hi,
>
> I wonder how and when a ruby object is destroyed? Where is the garbage
> collector? how it work?
> If I set a object to nil, does it means the GC will pick it up or ....?
>
> Can I set a Class to nil? then the GC "destroys" a class ???
>
> Thanks you very much
>
> Sayoyo

http://whytheluckystiff.net/articles/theFullyUpturn...
http://www.brpreiss.com/books/opus8/html/page414.html#SECTION001300000000...

(i just found 2nd link googling around, I haven't read carefully)

Robert Klemme

1/14/2006 11:04:00 AM

0

sayoyo@yahoo.com wrote:
> Hi,
>
> I wonder how and when a ruby object is destroyed?

How: the GC fetches a big hammer and smashes the object until it's flat
like a bit. Then it waits for a power surge to flip the bit in mem; then
the object is detroyed.

After destruction a finalizer is called if one is defined via
ObjectSpace.define_finalizer().

When: when there are no more hard references to the object and the GC
decides to do so. This is likely when mem gets low. The latest point in
time is termination of the Ruby interpreter:

12:01:46 [~]: ruby -e 'ObjectSpace.define_finalizer(Object.new) { puts
"destroyed" }'
destroyed

> Where is the garbage
> collector?

irb(main):001:0> GC
=> GC

You'll also find it in the C sources of Ruby.

> how it work?

IIRC it's mark and sweep.

> If I set a object to nil, does it means the GC will pick it up or
> ....?

You cannot set an object to nil. You can just set a reference to nil.
And whether an object is eligible for GC depends on whether there are any
references left. Note that there are also WeakReferences (like in Java)
that do not prevent an object from being GC'ed.

> Can I set a Class to nil? then the GC "destroys" a class ???

I don't think there is class garbage collection as in Java because class
objects are attached to consts and because of the dynamic nature of Ruby
you cannot simply resurrect a class instance (for example, there might be
methods defined on the class instance after the initial creation). So in
Ruby it would actually be a bad idea to have class GC IMHO.

> Thanks you very much

You're welcome.

Kind regards

robert

Malte Milatz

1/14/2006 5:24:00 PM

0

Robert Klemme:
> I don't think there is class garbage collection

>> 3.times { ObjectSpace.define_finalizer(Class.new) { puts 'destroyed' } }
=> 3
>> GC.start
destroyed
destroyed
destroyed
=> nil

Malte

Robert Klemme

1/14/2006 6:07:00 PM

0

Malte Milatz <mspam@gmx-topmail.de> wrote:
> Robert Klemme:
>> I don't think there is class garbage collection
>
>>> 3.times { ObjectSpace.define_finalizer(Class.new) { puts
>>> 'destroyed' } } => 3 GC.start
> destroyed
> destroyed
> destroyed
> => nil
>
> Malte

That's interesting. But these are anonymous classes and it's the
destruction on termination. You can produce the same with named classes:

$ ruby -e 'ObjectSpace.define_finalizer(String) { puts "destroyed" }'
destroyed

But I don't believe they will go away while the application is running. So
the situation is stil a bit different from Java I believe.

Kind regards

robert