5
votes

OS: Windows7 32bit main memory : 4GB ruby -v : ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]

# big.mkv file size : 1.45GB

ex1.rb

puts $$

File.open("D:/test/big.mkv", "rb") do |f|
  while buff = f.read(4096)
  end
end

sleep 1000

ex1.rb is OK!! memory usages is about 19,756 KB.

But...

ex2.rb

puts $$

th1 = Thread.new do
  loop do
    sleep 1
  end
end

File.open("D:/test/big.mkv", "rb") do |f|
  while buff = f.read(4096)
  end
end

th1.join

ex2.rb memory usages is increased continually... after all 1,937,948 KB

I have to use Thread.. Please.. Help Me!!

1
Please use spaces to format your code as code. - Andrew Grimm
Well, your thread never ends, so a join could take awhile. - Dave Newton
his question is clear isn't it ? Even if the thread never ends, the memory usage shouldn't be growing, so why ? - peter
I've tested this code with ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]and my memory usage didn't grow at all. Maybe it's a memory leak that has been fixed in next releases. - nolith

1 Answers

1
votes

There are file reading fixes in ruby 1.9. A script I wrote that reads a ton of data runs ~ 100x faster on ruby1.9. Please upgrade if possible, it's worth it.