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

S

sayoyo

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
 
G

Gene Tani

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/theFullyUpturnedBin.html
http://www.brpreiss.com/books/opus8/html/page414.html#SECTION0013000000000000000000

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

Robert Klemme

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
 
R

Robert Klemme

Malte Milatz said:
Robert Klemme:
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
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top