#! /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 count
      @count ||= 0
    end
    def to_s
      "%4d: "%(count.to_i) + 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
