From: Gennady Bystritsky Date: 2006-04-18T02:12:01+09:00 Subject: Re: IO not closed by GC But you do not even know when GC will be run, so in your case it might not even have kicked in. On the other hand, IO.popen has a nice block form that gurantees that an underlying IO object will be closed properly. You can rewrite your code as: os = IO.popen('uname -sr') { |io| io.readlines.to_s.strip } Gennady. > -----Original Message----- > From: Stephan Maka [mailto:stephan@spaceboyz.net] > Sent: Monday, April 17, 2006 9:56 > To: ruby-talk ML > Subject: IO not closed by GC > > Hello, > > I often use constructions like the following: > > os = IO.popen('uname -sr').readlines.to_s.strip > > I assume that the Garbage Collector throws away the IO > instance created by IO::popen, because it's simply not needed > after that line anymore. > > What I've experienced is, that it leaves a zombie for each > time I use such an instruction. > > --- > > loop { > zombies = IO.popen('ps ax|grep defunct|wc -l').readlines.to_s.strip > puts "Number of zombies: #{zombies}" > } > > --- > > This code produces about 100 defunct processes in about 2 seconds. > Operating system is FreeBSD 6.1-RC. > > However, if I close the IO object manually, defunct processes > stay constantly at 2-3. > > --- > > loop { > io = IO.popen('ps ax|grep defunct|wc -l') > zombies = io.readlines.to_s.strip > io.close > puts "Number of zombies: #{zombies}" > } > > --- > > Is this assumed behaviour or a bug? I guess there's a close(2) or > waitpid(2) missing in the IO code... > > Stephan. >