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
···
--
Posted via http://www.ruby-forum.com/.
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
--
Posted via http://www.ruby-forum.com/.
Hi Tridib,
You'll want to look into finalizers then. Be warned- they aren't the easiest things to work with.
Garth
On 22/12/11 13:21, Tridib Bandopadhyay wrote:
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
What's your use case?
On Wed, Dec 21, 2011 at 8:51 PM, Tridib Bandopadhyay <tridib04@gmail.com>wrote:
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,
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:
http://www.ruby-doc.org/core-1.9.3/ObjectSpace.html#method-c-define_finalizer
ObjectSpace.define_finalizer(self,
self.class.method(:finalize).to_proc)
end
# This should be a class method, not an instance method
# For reason,
http://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/
def self.finalize(id)
puts "Object #{id} dying at #{Time.now}"
end
end
DestroyObject.new
# Initiates garbage collection
# Source:
http://www.ruby-doc.org/core-1.9.3/ObjectSpace.html#method-c-garbage_collect
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
--
Posted via http://www.ruby-forum.com/.
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?
2. Is there any way to stop the ruby program in the middle of
execution?. Like sleep type of function
--
Posted via http://www.ruby-forum.com/.
Thank you..
Okay so how about if I wanna delete an array using finalizers?
Should I try array_name.new?
Regards
Tridib
--
Posted via http://www.ruby-forum.com/.
Often begin ensure is sufficient also. See
http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html
Cheers
robert
On Thu, Dec 22, 2011 at 9:12 AM, Josh Cheek <josh.cheek@gmail.com> wrote:
On Wed, Dec 21, 2011 at 8:51 PM, Tridib Bandopadhyay <tridib04@gmail.com>wrote:
Can anyone tell me how to check an object is and when it is freed by
Garbage Collector?What's your use case?
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
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
--
Posted via http://www.ruby-forum.com/\.
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.
2. 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
On Mon, Dec 26, 2011 at 9:18 PM, Tridib Bandopadhyay <tridib04@gmail.com> wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
I have some questions on the Code
class DestroyObject
def initialize
# Adds a proc as finalizer, this will be called after the object was
destroyed
# Source:
Module: ObjectSpace (Ruby 1.9.3)
ObjectSpace.define_finalizer(self,
self.class.method(:finalize).to_proc)
end
What does self and self.class.method referring to?
# This should be a class method, not an instance method
# For reason,
The Trouble with Ruby Finalizers | Mike Perham
def self.finalize(id)
puts "Object #{id} dying at #{Time.now}"
end
endDestroyObject.new
# Initiates garbage collection
# Source:
Module: ObjectSpace (Ruby 1.9.3)
ObjectSpace.garbage_collect
And which object is getting deleted? And who is deleting it? My debugged
free method or is it asking for Garbage Collect?
Thank You
Tridib
--
Posted via http://www.ruby-forum.com/\.
Hi Tridib,
Try "Ruby finalizers" and "Ruby define_finalizer" in Google- this should be a good starting point if you want to investigate further. ![]()
Also: Vimal's reply is a really good one, far better than mine. I'd *definitely* suggest reading through that. ![]()
Garth
On 23/12/11 14:26, Tridib Bandopadhyay wrote:
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
-----Messaggio originale-----
Da: Robert Klemme [mailto:shortcutter@googlemail.com]
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 Bandopadhyay <tridib04@gmail.com> 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.
2. 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