Hi,

On 24-Oct-07, at 3:09 PM, Robert Dober wrote:

> Sorry for replying to my own post, can somebody confirm please that
> the following code proves that Array#shift does not leak?
> Thx in advance
> def count
>   i = 0
>   ObjectSpace.each_object{ i += 1 }
>   i
> end
>
> a = []
> 10.times do
>   10_000.times do
>     a << "a"
>     a.shift
>   end
>   ObjectSpace.garbage_collect
>   p count
> end
> 381
> 382
> 382
> 382
> 382
> 382
> 382
> 382
> 382
> 382
>
> Robert
>


Try it this way:

class Thing
end

def count
   i = 0
   ObjectSpace.each_object(Thing){ i += 1 }
   i
end

a = []
10.times do
   10_000.times do
     a << Thing.new
   end
   ObjectSpace.garbage_collect
   10_000.times do
     a.shift
   end
   ObjectSpace.garbage_collect
   puts "count(1): #{count} size: #{a.size}"
   a.push(nil).pop
   ObjectSpace.garbage_collect
   puts "count(2): #{count} size: #{a.size}"

   10_000.times do
     a << Thing.new
   end
   ObjectSpace.garbage_collect
   10_000.times do
     a[0] = nil # <<<<<< !!!!
     a.shift
   end
   ObjectSpace.garbage_collect
   puts "count(3): #{count} size: #{a.size}"
end

count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0
count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0
count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0
count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0
count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0
count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0
count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0
count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0
count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0
count(1): 10000 size: 0
count(2): 0 size: 0
count(3): 0 size: 0

----
Bob Hutchison                  -- tumblelog at http:// 
www.recursive.ca/so/
Recursive Design Inc.          -- weblog at http://www.recursive.ca/ 
hutch
http://www.recursive.ca/       -- works on http://www.raconteur.info/ 
cms-for-static-content/home/