From: Robert Klemme Date: 2010-05-20T20:15:49+09:00 Subject: Re: method returning a hash, is the hash in the heap? 2010/5/20 Jian Lin : > Robert Klemme wrote: > >> The lifetime of an instance is more accurately described as "from its >> creation until GC decides to remove it".  Of course, the earliest >> point in time when GC can decide to remove it is after the last >> reference to the instance has gone.  (Note there is a special case >> with WeakReference but I don't want to complicate things too much >> initially. There are also ways to access an instance during the period >> between "last ref gone" and "GC removed it" via ObjectSpace but this >> is not something you should rely on.) > > yes, because previous, I was thinking that any local variables will get > erased and the stack will shrink, so in this case, the hash seems valid > and it is actually a new object creation, something that is like a > malloc()... and it resides in the heap. Exactly. > for the WeakReference... the wiki explanation is "weakly reachable"... I > thought it can be garbage collected when there is not usable reference > that can reach that object... but what if it is still "reachable, but > weakly", then it can still be garbage collected? Yes. >  Then won't the > reference to it and its usage cause a segmentation fault or similar > error? No, you WeakRef#weakref_alive? will simply return false and WeakRef#__getobj__ will just throw WeakRef::RefError which you will also see for any other method. irb(main):018:0> r.weakref_alive? => false irb(main):019:0> r.to_s WeakRef::RefError: Invalid Reference - probably recycled from (irb):18 from /opt/bin/irb19:12:in `
' irb(main):020:0> The Ruby heap is not exactly the same as a heap in languages like C: in C you directly dereference memory pointers and if there is an error you'll get a segfault. In Ruby (as in Java as well) the runtime system manages memory accesses and you will never see a segfault (unless there is a bug in the runtime or in a C extension). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/