On Sun, May 27, 2012 at 4:10 PM, Damián M. González <[email protected]> wrote: > Hello fellows, I'm wondering if I should set to nil any variables that I > will not use anymore for the sake of save memory system, I didn't see > this way of work but I'm curious of what you can say. > I'm not sure about how GC works, tell me if I'm wrong: the GC will > erase in memory those "labels" that are pointing to nil, so, if I set to > nil any variables, that space in memory will be free again. > I'll aprreciate any explanation.
Depends on whether we are talking instance variables or local variables. Local variables are automatically out of scope once the method terminates (either via return or throw). As Peter said, if that was the last reference to an object it is read for collection. When that happens is a different story. If you have member variables which are not needed any more then you can set them to nil. Usually though, most member variables are still needed to represent state of an object and if the object itself is no longer reachable then those references are not life any more - so explicit "clearing" (via setting to nil) is not needed most of the time. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ -- You received this message because you are subscribed to the Google Groups ruby-talk-google group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at https://groups.google.com/d/forum/ruby-talk-google?hl=en
