From: Brian Candler Date: 2011-08-02T16:36:14+09:00 Subject: Re: system stack error in ObjectSpace.each_object luke gruber wrote in post #1014312: > 1) For the container idea, how would you go about doing this? Others have answered this already. (There's weakref if you really need a container which doesn't prevent garbage collection, but I've never had to use it) > 2) As for ObjectSpace returning dead objects, is there a way to force GC > during runtime? You can force garbage collection, but the issue is rather that the garbage collector is conservative; any bit of junk sitting on the stack which looks like it *might* be a reference to an object is treated as if it is, just to be on the safe side. This means that some objects which really could be garbage-collected, aren't. You can write some simple test programs using ObjectSpace to count the number of objects of each class, create some and drop the refs, then run GC.start, then count them again. It's not unusual to see one or two objects left in a particular class, even though there are no live refs. > Also, does taking all variables that point to an object > and pointing them to nil effectively dangle an object like this? Explicitly destroying all references to an object will normally make it eligible for garbage collection: it's a mark-and-sweep collector, so the object won't be found during the mark phase. But as I say, a few objects may be considered live when technically they aren't. -- Posted via http://www.ruby-forum.com/.