From: Eric Mahurin Date: 2005-10-10T11:07:59+09:00 Subject: Re: using lambda/Proc can prevent a lot of garbage collection --- "David A. Black" wrote: > Hi -- > > On Sun, 9 Oct 2005, Eric Mahurin wrote: > > > Here is an example where a Proc can cause a memory leak: > > > > ruby -e ' > > n=2**13;squares=(1..n).map{|i|a=(1..i).to_a;lambda{i*i}}; > > > IO.readlines("/proc/#{Process.pid}/status").grep(/VmSize/).display' > > VmSize: 169312 kB > > > > You wouldn't expect each of these lambda's to need the > local > > "a", but the binding holds it. Right now, the programmer > needs > > to specifically think about this and clear these unexpected > > object references by hand: > > > > ruby -e ' n=2**13;squares = > > (1..n).map{|i|a=(1..i).to_a;f=lambda{i*i};a=nil;f}; > > > IO.readlines("/proc/#{Process.pid}/status").grep(/VmSize/).display' > > VmSize: 10644 kB > > That's an interesting illustration, but I don't think I'd > call it a > memory leak, since it's working as advertised. (At least, > "memory > leak" to me implies something going wrong under the hood, so > to > speak.) Well, C's malloc and free also work as advertised. And you can easily make a C program that has a memory leak if you don't pair these properly. You can blame it on the C language/library or you can blame it on the C programmer, but either way there is a memory leak when you don't pair these properly. I see the above case as the same. Here is the definition of "memory leak" I found on http://www.webopedia.com/TERM/M/memory_leak.html : "A bug in a program that prevents it from freeing up memory that it no longer needs" In the first example above that is what is going on. The program is not freeing memory it no longer needs. Again, you can blame this on the ruby programmer or the ruby langauge. Since this is usually unwanted and/or unexpected, I choose to blame the language. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com