From: Brian Candler Date: 2008-09-22T23:08:20+09:00 Subject: Re: for or each? Sylvain Joyeux wrote: > To cut down the discussion, > ObjectSpace#each_object does *not* give you those, so you can't count > them with it. OK, I see that certain objects are not yielded, including T_SCOPE. On the other hand, observe that the following program doesn't leak memory: def foo :dummy end GC.disable (1..10_000_000).each { |i| foo } puts `ps auxwww | grep ruby | grep -v grep` puts "Press enter" STDIN.gets On the two machines I tried the RSS is 3MB, regardless of how big I make the loop. (That's 1.8.4 stock Ubuntu Dapper, and 1.8.6p114 compiled from source) So are you sure a scope is created every time round the loop - not just on the first invocation? The following program *does* consume memory: def foo :dummy end GC.disable (1..1_000_000).each { |x| (1..1).each { |y| foo } } puts `ps auxwww | grep ruby | grep -v grep` puts "Press enter" STDIN.gets That is: I'm happy to accept that every time the inner loop starts, it creates a new scope (since the block is a closure with a different value of x bound to it) But if I change the inner loop to (1..10).each { |y| foo } the memory consumption is the same. So whether a block is invoked once or 10 times makes no difference to memory usage. -- Posted via http://www.ruby-forum.com/.