[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Garbage collection and Arrays

Aaron Patterson

1/12/2007 6:59:00 AM

Hi, I seem to be running in to some garbage collection weirdness.
Hopefully someone will have some insight for me.... My sample script
seems to leak Strings when I use shift on an Array, but not pop:

array = []
10.times { array << "foo" }
10.times { array.pop } # Change this to shift to see a leak

o_count = Hash.new(0)
GC.start
ObjectSpace.each_object { |o| o_count[o.class] += 1 }

o_count.sort_by { |k,v| -v}.each { |k,v| puts "#{k}: #{v}" }

Is this a bug? Or is there something I'm missing? Thanks!

--Aaron

--
Aaron Patterson
http://tenderlovem...

5 Answers

Gregory Brown

1/12/2007 7:06:00 AM

0

On 1/12/07, Aaron Patterson <aaron_patterson@speakeasy.net> wrote:
> Hi, I seem to be running in to some garbage collection weirdness.
> Hopefully someone will have some insight for me.... My sample script
> seems to leak Strings when I use shift on an Array, but not pop:

http://recursive.ca/hutch/index...

Aaron Patterson

1/12/2007 7:31:00 AM

0

On Fri, Jan 12, 2007 at 04:05:55PM +0900, Gregory Brown wrote:
> On 1/12/07, Aaron Patterson <aaron_patterson@speakeasy.net> wrote:
> >Hi, I seem to be running in to some garbage collection weirdness.
> >Hopefully someone will have some insight for me.... My sample script
> >seems to leak Strings when I use shift on an Array, but not pop:
>
> http://recursive.ca/hutch/index...
>

Thanks!

--
Aaron Patterson
http://tenderlovem...

khaines

1/12/2007 2:14:00 PM

0

Bernard Kenik

1/14/2007 6:22:00 AM

0


Aaron Patterson wrote:
> On Fri, Jan 12, 2007 at 04:05:55PM +0900, Gregory Brown wrote:
> > On 1/12/07, Aaron Patterson <aaron_patterson@speakeasy.net> wrote:
> > >Hi, I seem to be running in to some garbage collection weirdness.
> > >Hopefully someone will have some insight for me.... My sample script
> > >seems to leak Strings when I use shift on an Array, but not pop:
> >
> > http://recursive.ca/hutch/index...
> >
>
> Thanks!
>
> --
> Aaron Patterson
> http://tenderlovem...

The workaround to eliminate leakage with array.shift works except that
shift no longer returns the shifted element and instead returns nil

The following class allows shift to work normally without the leakage
.... I know it is only a temporary work around until the ruby developers
get around to fix the c code

#### Array#shift ###############
class Array
alias :orig_shift :shift
def shift
return nil if self.empty?
ret = self[0]
self[0] = nil
self.orig_shift
return ret
end
end

###### code to show that the leakage has been eliminated
separator = "---"
o_count = Hash.new(0)
GC.start

ObjectSpace.each_object { |o| o_count[o.class] += 1 }
o_count.sort_by { |k,v| -v}.each { |k,v| puts "#{k}: #{v}" }
o_count = nil
puts separator

array = []
10.times { array << "foo" }
10.times { array.pop } # Change this to shift to see a leak

o_count = Hash.new(0)
GC.start
ObjectSpace.each_object { |o| o_count[o.class] += 1 }
o_count.sort_by { |k,v| -v}.each { |k,v| puts "#{k}: #{v}" }
array = nil
o_count = nil

puts separator
array = []
10.times { array << "foo" }
10.times { array.shift } # Change this to shift to see a leak

o_count = Hash.new(0)
GC.start
ObjectSpace.each_object { |o| o_count[o.class] += 1 }
o_count.sort_by { |k,v| -v}.each { |k,v| puts "#{k}: #{v}" }

__END__

####### test results only pertinent data shown
NEW shift method

before pop after pop
before shift after shift
String: 1746 1764 1764
Array: 70 71 72

OLD shift methed -- Class Array commented out
before pop after pop
before shift after shift
String: 1746 1764 1774
Array: 70 71 72


################## code to show that Array#shift behaves as described
in the ri documentation

class Array
alias :orig_shift :shift
def shift
return nil if self.empty?
ret = self[0]
self[0] = nil
self.orig_shift
return ret
end
end

a = (1..5).to_a

1.upto(10) do |i|
x = a.shift

puts "#{i}: \tx: #{x.inspect} \ta: #{a.inspect}"
end

__END__


1: x: 1 a: [2, 3, 4, 5]
2: x: 2 a: [3, 4, 5]
3: x: 3 a: [4, 5]
4: x: 4 a: [5]
5: x: 5 a: []
6: x: nil a: []
7: x: nil a: []
8: x: nil a: []
9: x: nil a: []
10: x: nil a: []

Irish Mike

3/21/2012 5:03:00 PM

0

On Mar 21 2012 4:57 AM, c s wrote:

> On Mar 20, 12:07?pm, wolfagain <w...@provide.net> wrote:
> > So all that RANTING like a fool for Union Rights in Wisconsin and Ohio
> > was just Ed's way of thanking union employees for all that Cash. He
> > claimed he gave it all to charity...BUT records indicate he gave only
> > 100K of it to charity.
> >
> > Who ELSE is Big Mouth Ed getting cash from???!
>
> kinda funny that a guy with a show on a few hundred radio stations and
> on a low ratings cable network ( fox news scum and rightie radio scum
> say this all the time ) are so worried about Big Eddy...HaHaHa..Kinda
> making you rightwing assholes squirm isn't he? Don't like to take the
> heat just give it? Your God Limbaugh and the fox news lying bastards
> must think hes more than just small potatoes. Keep pouring it on Big
> Ed and MSNBC..shove it up their rightwing asses!!!!!

LOL! I'm just guessing here but does "c s" stand for "crazy shit"?

Irish Mike

"It's not God bless America, it's God damn America!" Jeremiah Wright,
Obama's mentor and spiritual leader for more than 20 years.

---?