2006/7/6, ara.t.howard / noaa.gov <ara.t.howard / noaa.gov>: > On Thu, 6 Jul 2006, Tom Rauchenwald wrote: > > > ara.t.howard / noaa.gov writes: > > > >> any idea why this script slows downs drastically as it runs and seems to leak > >> memory? > > > > First, I'm not a Ruby expert or something, but.. > > > > [Code skipped] > > > >> switch = Switch.new > > > > Here you create the switch. > > > >> toggle = SwitchToggle.new switch > > > > Here, toggle is created and registered as listener (in the constructor) > > > >> switch.on! > > > > So now you switch it on. > > Toggle gets notified, and creates a thread that switches it off. > > But when that Thread switches it off, toggle gets notified, and starts a > > thread that switches it on again.. > > Basically it's an infinite loop, and i suspect it's leaking because GC > > doesn't kick in. > > it would be, but the synchronize prevents two threads from entering any of the > Switch methods at a time so only thread can actually be running at once. Yes, but they are still queued up against the mutex. I completely agree with Tom's analysis. Having an Observer trigger the notification again is definitively a bad idea - at least if events are not queued up. Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/