object destruction

Discussion in 'Ruby' started by Joey Marino, Mar 22, 2008.

  1. Joey Marino

    Joey Marino Guest

    [Note: parts of this message were removed to make it a legal post.]

    I have a list of objects and after each initialization a few methods are
    called on them. They are inherently different but perform the same tasks on
    a different set of data. These tasks require ALOT of memory. After the
    object has performed its tasks on the data, it is no longer needed and can
    be destroyed. This way, its memory space can be used for the next object. I
    am having difficulty destroying these objects since there is no destructor
    capability in ruby that I can find. I have tried using the garbage collector
    like follows:

    -------------------------------------------------
    include GC
    GC.enable
    GC.starto

    obj = Object1.new
    obj.method
    obj.finalize

    ObjectSpace.garbage_collect

    obj2 - Object2.new
    obj2.method
    obj.finalize

    ObjectSpace.garbage_collect

    ####################

    class Object
    def initialize
    ObjectSpace.define_finalizer(self,self.finalize)
    end

    def finalize
    proc{ |id| puts "finalizing #{id}" }
    end
    end

    class Object1 < Object
    def initialize
    super
    @some_attribute = 1
    end
    end

    class Object2 < Object
    def initialize
    super
    @some_attribute = 2
    end
    end
    _________________________

    I am assuming this does not destroy the objects since there is no output and
    memory is not released. I am not even getting output of a finalized object
    at the end of runtime.
    Am I doing something wrong or just missing out a key concept here?
    --
    Joey Marino
     
    Joey Marino, Mar 22, 2008
    #1
    1. Advertising

  2. Joey Marino

    ara howard Guest

    On Mar 21, 2008, at 8:49 PM, Joey Marino wrote:

    > ObjectSpace.define_finalizer(self,self.finalize)


    finalizers cannot, in any way , refer to the object being destroyed
    otherwise the reference prevents the object from ever being freed.

    why not simply do

    if fork do
    memory_intensive_work
    end

    Process.wait

    this way memory will always be freed.

    regards.

    a @ http://drawohara.com/
    --
    sleep is the best meditation.
    h.h. the 14th dalai lama
     
    ara howard, Mar 22, 2008
    #2
    1. Advertising

  3. On 22.03.2008 03:49, Joey Marino wrote:
    > [Note: parts of this message were removed to make it a legal post.]
    >
    > I have a list of objects and after each initialization a few methods are
    > called on them. They are inherently different but perform the same tasks on
    > a different set of data. These tasks require ALOT of memory. After the
    > object has performed its tasks on the data, it is no longer needed and can
    > be destroyed. This way, its memory space can be used for the next object. I
    > am having difficulty destroying these objects since there is no destructor


    You do not need to destroy objects explicitly - GC will take care. It's
    more important to make sure all resources (such as file handles) are
    released and you do not hold longer onto your instances than needed.

    > capability in ruby that I can find. I have tried using the garbage collector
    > like follows:
    >
    > -------------------------------------------------
    > include GC
    > GC.enable
    > GC.starto
    >
    > obj = Object1.new
    > obj.method
    > obj.finalize
    >
    > ObjectSpace.garbage_collect
    >
    > obj2 - Object2.new
    > obj2.method
    > obj.finalize
    >
    > ObjectSpace.garbage_collect
    >
    > ####################
    >
    > class Object
    > def initialize
    > ObjectSpace.define_finalizer(self,self.finalize)
    > end
    >
    > def finalize
    > proc{ |id| puts "finalizing #{id}" }
    > end
    > end


    The code above does not work as intended as Ara has pointed out.

    > I am assuming this does not destroy the objects since there is no output and
    > memory is not released. I am not even getting output of a finalized object
    > at the end of runtime.
    > Am I doing something wrong or just missing out a key concept here?


    Yes, see above.

    Kind regards

    robert
     
    Robert Klemme, Mar 22, 2008
    #3
  4. Joey Marino

    Robert Dober Guest

    On Sat, Mar 22, 2008 at 3:49 AM, Joey Marino <> wrote:
    > I have a list of objects and after each initialization a few methods are
    > called on them. They are inherently different but perform the same tasks on
    > a different set of data. These tasks require ALOT of memory. After the
    > object has performed its tasks on the data, it is no longer needed and can
    > be destroyed. This way, its memory space can be used for the next object. I
    > am having difficulty destroying these objects since there is no destructor
    > capability in ruby that I can find. I have tried using the garbage collector
    > like follows:
    >
    > -------------------------------------------------
    > include GC
    > GC.enable
    > GC.starto
    >
    > obj = Object1.new
    > obj.method
    > obj.finalize
    >
    > ObjectSpace.garbage_collect
    >
    > obj2 - Object2.new
    > obj2.method
    > obj.finalize
    >
    > ObjectSpace.garbage_collect
    >
    > ####################
    >
    > class Object
    > def initialize
    > ObjectSpace.define_finalizer(self,self.finalize)
    > end
    >
    > def finalize
    > proc{ |id| puts "finalizing #{id}" }
    > end
    > end
    >
    > class Object1 < Object
    > def initialize
    > super
    > @some_attribute = 1
    > end
    > end
    >
    > class Object2 < Object
    > def initialize
    > super
    > @some_attribute = 2
    > end
    > end
    > _________________________
    >
    > I am assuming this does not destroy the objects since there is no output and
    > memory is not released. I am not even getting output of a finalized object
    > at the end of runtime.
    > Am I doing something wrong or just missing out a key concept here?
    > --
    > Joey Marino
    >


    Hopefully this is helpful
    destroyed_objects = 0
    finalizer = proc { destroyed_objects += 1 } # in this scope no
    instance of MyObject is caught in the closure!!!
    MyObject = Class::new( Object ) {
    define_method :initialize do
    ObjectSpace.define_finalizer self, finalizer
    end
    }
    a = Array.new(100_000){ MyObject.new }
    GC.start

    puts "Finalized objects #{destroyed_objects}"
    a.each_index do |idx|
    a[idx] = nil
    end
    puts "Finalized objects #{destroyed_objects}"
    GC.start
    puts "Finalized objects #{destroyed_objects}"

    existing_objects = 0
    ObjectSpace.each_object( MyObject ) do
    existing_objects += 1
    end
    puts "Existing objects #{existing_objects}"


    Cheers
    Robert
    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Mar 22, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johny smith

    question about object destruction

    johny smith, May 9, 2004, in forum: C++
    Replies:
    4
    Views:
    336
    Andrey Tarasevich
    May 10, 2004
  2. EN

    object destruction

    EN, Mar 15, 2005, in forum: C++
    Replies:
    3
    Views:
    338
    Leon Mergen
    Mar 15, 2005
  3. Pablo
    Replies:
    6
    Views:
    343
    Howard
    Aug 18, 2006
  4. V Patel
    Replies:
    5
    Views:
    318
    Yahooooooooo
    Jan 30, 2007
  5. kalki70

    scope and object destruction

    kalki70, Feb 7, 2007, in forum: C++
    Replies:
    2
    Views:
    321
    kalki70
    Feb 7, 2007
Loading...

Share This Page