From: nobu.nokada@... Date: 2003-09-18T09:16:55+09:00 Subject: Re: Testing for memory leaks Hi, At Thu, 18 Sep 2003 06:38:24 +0900, Berger, Daniel wrote: > Being as I write a lot of extensions, I've discovered that it's best to > check for memory leaks. The way I'm doing this now is something like this: This is the script "memusage.rb" I'm using on Linux. Requires procfs mounted at /proc. #! /usr/bin/ruby module Memory keys = [] vals = [] IO.foreach("/proc/self/status") do |l| /^Vm(\w+):\s+(\d+)/ =~ l or next keys << $1.downcase.intern vals << $2.to_i end Status = Struct.new(*keys) Status.module_eval do const_set(:Header, " "+keys.collect {|k| k.to_s.upcase.rjust(6)}.join) const_set(:Format, "%6d") end init = Status.new(*vals) class Status @@count = 0 def initialize @count = @@count += 1 IO.foreach("/proc/self/status") do |l| /^Vm(\w+):\s+(\d+)/ =~ l or next self.__send__($1.downcase+"=", $2.to_i) end end def to_s "%4d: "%@count + collect {|n| Format % n}.join end end puts Status::Header, init end if __FILE__ == $0 10.times do puts Memory::Status.new GC.start end end > Is there a better and/or more streamlined way to do this? Any chance of > adding assert_no_memory_leak to TestUnit, for example? I don't guess that assertion is easy. Generally, it's difficult to determin the memory is really leaked or not. -- Nobu Nakada