From: David Beswick <"david mail"@...> Date: 2008-04-15T21:10:16+09:00 Subject: Re: WeakRef -- excessive memory usage? 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? David YuDerek wrote: > [Note: parts of this message were removed to make it a legal post.] > > puts 'weakref'r = require 'weakref'puts rs = 'hello'a = []GC.disable #<--------5.times { gets puts "100times" 100.times { WeakRef.new(s) } puts "end times" > } > In fact, GC works. > GC is enabled by default, > You may not need to use GC.start to start GC, it seems that ruby deals with GC well. > Without GC.disable, the memory usage is about 10,500K every iteration. > But when you disable GC, the memory usage is as below: > before > 5,200k > after > 1 77,764k2 145,976k > 3 231,088k > 4 277,376k > > I think ruby actually get these space back, > but it keeps these space somewhere, > so it will be fast next time you make some new things without getting it from OS. > > > >> Date: Sat, 12 Apr 2008 23:05:05 +0900> From: "david mail"@ruby-lang.org> Subject: WeakRef -- excessive memory usage?> To: ruby-talk@ruby-lang.org> > Hi everyone, I recently had a problem with memory usage in my ruby 1.8.6 > program. It had ballooned from about 65mb after startup to 300mb. I > tracked the problem down to my usage of WeakRefs. Storing object_ids > instead got the memory usage back to where it was.> > In the following example I'm checking memory usage with Windows Task > Manager.> > > > irb> > irb(main):001:0> require 'weakref'> => true> irb(main):002:0> s = 'hello'> => "hello"> irb(main):003:0> a = []> => []> > (memory usage is 4,171k at this point)> > irb(main):004:0> 100.times { a << WeakRef.new(s) }> => 100> > (memory usage is now 54,380k)> > > Repeated calls to GC.start doesn't reduce the memory usage at this > point. Am I missing something?> > David> > _________________________________________________________________ > ������MSN�������������������������������������� > http://mobile.msn.com.cn/