Free an Object

Hello

Can anyone tell me how to check an object is and when it is freed by
Garbage Collector?

Ruby Version- 1.8.6

Regards

Tridib

Hi Tridib,

You’ll want to look into finalizers then. Be warned- they aren’t the
easiest things to work with.

Garth

On Wed, Dec 21, 2011 at 8:51 PM, Tridib B.
[email protected]wrote:

What’s your use case?

On Thu, Dec 22, 2011 at 9:12 AM, Josh C. [email protected]
wrote:

On Wed, Dec 21, 2011 at 8:51 PM, Tridib B. [email protected]wrote:

Can anyone tell me how to check an object is and when it is freed by
Garbage Collector?
What’s your use case?

Often begin ensure is sufficient also. See
http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html

Cheers

robert

Garthy D wrote in post #1037805:

Hi Tridib,

You’ll want to look into finalizers then. Be warned- they aren’t the
easiest things to work with.

Garth

Hello… Thanks for the reply

Sorry But I didn’t get what you meant by finalizers. Where can I find
it?

Regards

Tridib

Hi Tridib,

I’ll try to explain what Garth is trying to say.

There is a module called ObjectSpace in ruby
(http://corelib.rubyonrails.org/classes/ObjectSpace.html#M001607). This
will help program to interact with the garbage collector. So using an
ObjectSpace, you can create a finalizer proc to free the resources of an
object.

By default, objects are destroyed by the Garbage collector. But still
you can ensure using ObjectSpace module.

Here is an example:

class DestroyObject
def initialize
# Adds a proc as finalizer, this will be called after the object was
destroyed
# Source:

ObjectSpace.define_finalizer(self,
self.class.method(:finalize).to_proc)
end

This should be a class method, not an instance method

For reason,

def self.finalize(id)
puts “Object #{id} dying at #{Time.now}”
end
end

DestroyObject.new

Initiates garbage collection

Source:

ObjectSpace.garbage_collect

I hope it clears your query on finalizer.

Even I’m not expert on this, but this thread helped me to learn about
this. If anyone finds any mistakes, welcome to correct.

Cheers,
Vimal

Hello Vimal

Thank you for the details. It was really helpful.Now let me give my
details of what I am trying to do.

I have made an extension of my own which will help the user to manually
allocate and deallocate an object using a ruby program in a stack which
my extension file is handling, and my extension does work properly.

Now I debugged some code in gc.c file and came to the point that Garbage
Collector is also marking that same object and hopefully allocating
also(which I am not sure) as a result there will duplicate objects in
the memory.To check this I asked for when the garbage collector is
freeing the memory.Is there any other way I can check if the garbage
collector is storing the object in the memory or not?

Also, I have some few questions also-

  1. Why some methods/Functions inside some file starts with the name–

rb_alloc()… what does the “rb” in the method name means?

  1. Is there any way to stop the ruby program in the middle of
    execution?. Like sleep type of function

Hi Tridib,

Try “Ruby finalizers” and “Ruby define_finalizer” in Google- this should
be a good starting point if you want to investigate further. :slight_smile:

Also: Vimal’s reply is a really good one, far better than mine. I’d
definitely suggest reading through that. :slight_smile:

Garth

On Mon, Dec 26, 2011 at 9:18 PM, Tridib B.
[email protected] wrote:

I have made an extension of my own which will help the user to manually
allocate and deallocate an object using a ruby program in a stack which
my extension file is handling, and my extension does work properly.

Apparently not because:

Now I debugged some code in gc.c file and came to the point that Garbage
Collector is also marking that same object and hopefully allocating
also(which I am not sure) as a result there will duplicate objects in
the memory.To check this I asked for when the garbage collector is
freeing the memory.Is there any other way I can check if the garbage
collector is storing the object in the memory or not?

I do not know the code but apparently you have managed to mix up
Ruby’s allocation with your own. It seems you want to build an
optimization by placing some objects on the stack. However, you need
to be aware that this approach will fail the very moment that you pass
a reference of your stack stored object to another object which
resides on the heap (i.e not on the stack).

Also, I have some few questions also-

  1. Why some methods/Functions inside some file starts with the name–

rb_alloc()… what does the “rb” in the method name means?

“rb” means “Ruby”. This is the C version of a namespace.

  1. Is there any way to stop the ruby program in the middle of
    execution?. Like sleep type of function

Sleeping is not exactly stopping. If you work with a C debugger you
only need to set a breakpoint at the proper location.

Cheers

robert

Thank you…

Okay so how about if I wanna delete an array using finalizers?

Should I try array_name.new?

Regards

Tridib

-----Messaggio originale-----
Da: Robert K. [mailto:[email protected]]
Inviato: marted 27 dicembre 2011 11:57
A: ruby-talk ML
Oggetto: Re: Free an Object

On Mon, Dec 26, 2011 at 9:18 PM, Tridib B.
[email protected]
wrote:

I have made an extension of my own which will help the user to
manually allocate and deallocate an object using a ruby program in a
stack which my extension file is handling, and my extension does work
properly.

Apparently not because:

Now I debugged some code in gc.c file and came to the point that
Garbage Collector is also marking that same object and hopefully
allocating also(which I am not sure) as a result there will duplicate
objects in the memory.To check this I asked for when the garbage
collector is freeing the memory.Is there any other way I can check if
the garbage collector is storing the object in the memory or not?

I do not know the code but apparently you have managed to mix up Ruby’s
allocation with your own. It seems you want to build an optimization by
placing some objects on the stack. However, you need to be aware that
this
approach will fail the very moment that you pass a reference of your
stack
stored object to another object which resides on the heap (i.e not on
the
stack).

Also, I have some few questions also-

  1. Why some methods/Functions inside some file starts with the name–

rb_alloc()… what does the “rb” in the method name means?

“rb” means “Ruby”. This is the C version of a namespace.

  1. Is there any way to stop the ruby program in the middle of
    execution?. Like sleep type of function

Sleeping is not exactly stopping. If you work with a C debugger you
only
need to set a breakpoint at the proper location.

Cheers

robert


remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Riccione Hotel 3 stelle in centro: Pacchetto Capodanno mezza pensione,
animazione bimbi, zona relax, parcheggio. Scopri l’offerta solo per
oggi…
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid983&d)-12