From: Eleanor McHugh Date: 2009-04-02T17:27:04+09:00 Subject: Re: Nilling for GC On 2 Apr 2009, at 08:19, 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 You could write something like: def thread_dead? worker (Time.now - worker[:started_at] > 5) && (worker[:thread].status == false) end @semaphore.synchronize do hs.delete_if do |k, v| v.map! { |worker| thread_dead?(worker) ? (cleaned += 1; nil) : worker }.compact! v.empty? end end Ellie Eleanor McHugh Games With Brains http://slides.games-with-brains.net ---- raise ArgumentError unless @reality.responds_to? :reason