From: Rick DeNatale Date: 2008-04-15T22:39:57+09:00 Subject: Re: WeakRef -- excessive memory usage? 2008/4/15 mail"@ruby-lang.org David Beswick <"david>: > Thanks for your reply Derek. I can definitely see that the intepreter is > freeing memory over the course of the run. > > In my original example, I made sure to add WeakRefs to an array to > ensure that they wouldn't get garbage collected. The reason I still > think WeakRef is using an unusual amount of memory is that when I assign > that array reference to nil on the completion of the run and call > GC.start, memory usage goes down from around 150mb to 20mb (for example). > > So, I can see that ruby hasn't completely shrunk its heap back down to > the original size (as you say, it must reserve the memory as per its > memory management algorithm). But this also means that the WeakRef > objects were using a large amount of the heap, since the majority of the > heap was released once the references to the WeakRefs were released. > > What it is about WeakRef's implementation that might cause this? A few observations: 1) The job of the GC is to preserve any reachable objects while reclaiming the space taken up by non-reachable objects. The first part of this is a bit more important than the second, since freeing a referenced object can lead to mysterious errors when a reference is followed. 2) Irb is not a good tool for doing experiments with GC. The reason is that it has references itself which can cause objects to hang around which you might not expect. 3) WeakRefs themselves take up space. In the 1.8 implementation each WeakRef is a ruby object which will have a class pointer, and an instance variable hash to hold it's one instance var which hangs on to the object id of the object it's referenceing. Then there are two class variables which point to hashes. One hash has an entry for each WeakRef object and maps it to the id of the object it references, the other hash maps the object id of each object referenced by a WeakRef to an array of WeakRefs referring to that object. The entries in those hashes are set when the WeakRef is created, and manipulated by a finalizer, which deletes the appropriate entries in each of the hashes when a weakly referenced object is GCed. I haven't added it all up but I suspect that each WeakRef takes more storage than your string "Hello" -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/