From: Toby DiPasquale Date: 2006-03-18T04:26:01+09:00 Subject: Re: Writing long-running daemons without memory leaks? ahoward wrote: > if you looking for why your code is so big look to you handler: > changing it > to > > Thread.start(s){|s| loop{ s.read(8192) or break }} > > will result in runs that look like this: Interesting. > so it's the handler/Buffer class that consumes (but does't leak) so much > memory. > > in any case, i think you are making the code too hard : ruby already > does > massive internal buffering - you can do what you want with less code i > think: > > harp:~ > cat netstring.rb > class NetString < ::String > def initialize arg = "" > arg.respond_to?("read") ? read(arg) : super(arg) > end > def clear > self.replace "" > end > def read port > clear > self.replace(port.read(port.read(4).unpack("N").first)) > end > def write port > port << [size].pack("N") > port << self > end > end > > harp:~ > cat a.rb > require 'socket' > require 'netstring' > > STDOUT.sync = true > > ss = TCPServer.new '127.0.0.1', 10001 > > loop do > s = ss.accept > puts "got connection" > Thread.start(s) do |s| > File.open("/dev/null", "w") do |f| > ns = NetString.new s > puts "received #{ ns.size } bytes" > written = f.write ns > puts "wrote #{ written } bytes" > end > end > end > > > harp:~ > cat b.rb > require 'socket' > require 'netstring' > > loop do > TCPSocket.open('127.0.0.1', 10001) do |t| > NetString::new("a" * 65536).write t > end > end > Thanks, a. I will try to incorporate your suggestions when I get it working again. -- Toby DiPasquale -- Posted via http://www.ruby-forum.com/.