From: Rick DeNatale Date: 2009-04-02T16:57:33+09:00 Subject: Re: Nilling for GC --001636163c5f422ee604668dcab5 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Thu, Apr 2, 2009 at 3:19 AM, Paganoni wrote: > Hi, I've read that > http://whytheluckystiff.net/articles/theFullyUpturnedBin.html. > I have a background script that runs continuously and threads some tasks > on demand. I keep track of the launched tasks but, by keeping those > tracks, I certainly prevent tasks classes instances and threads > instances to be discarded by the garbage collector. > > Data are stored like that : > > @workers[:one_category] = [] > @workers[:one_category] << {:started_at => Time.now, :thread => th} > > So, I'm trying to setup a cleaner that would nil old references but I'm > lost in maps : > > @semaphore.synchronize do > @workers.map{|k,v| v.map{|worker| (Time.now - worker[:started_at] > 5 > && worker[:thread].status == false) ? (cleaned +=1; nil) : worker}} > end > > When condition is true, cleaned is incremented but the nil does not > replace @workers entry. > > So, I did another test : > hs = {} > hs[:toto] = [] > hs[:toto] << {:kk => 28, :so => 'yes'} > hs[:toto] << {:kk => 30} > hs[:tata] = [] > hs[:tata] << {:kk => 2} > > hs = hs.map{|k,v| v.map {|a| a[:kk] > 28 ? nil : a} } > pp hs displays > [[{:kk=>28, :so=>"yes"}, nil], [{:kk=>2}]] > > This one works right, for each hs entry, array is nilled if :kk value is > >> to 28 >> > > Obviously there is a mistake somewhere in the first code source but I don't > find it. > > Any help appreciated, thank you > @workers.map{|k,v| v.map{|worker| (Time.now - worker[:started_at] > 5 # ... vs. hs = hs.map{|k,v| v.map {|a| a[:kk] > 28 ? nil : a} } In the first case you create a new array from the nested map calls and then THROW IT AWAY, @workers still refers to the old array. In the second you compute a new array and assign it to hs. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale --001636163c5f422ee604668dcab5--