Module: Ruote::Mem

Defined in:
lib/ruote/util/look.rb

Overview

Some utilities for mem usage analysis

Class Method Summary collapse

Class Method Details

.countObject

Returns a Hash : classname => [ count, maxsize, totalsize, avgsize ]

The relative size of an object is computed with Marshal.dump(o).size

This uses ObjectSpace.

see www.ruby-forum.com/topic/186339 for better options.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruote/util/look.rb', line 69

def self.count

  uninteresting = [
    Array, String, Hash, Set, Module, Range, Float, Bignum
  ]

  h = {}

  ObjectSpace.each_object do |o|

    next if uninteresting.include?(o.class)

    stats = h[o.class.to_s] ||= [ 0, 0, 0 ]
    size = (Marshal.dump(o).size rescue 1)

    stats[0] += 1
    stats[1] = size if size > stats[1]
    stats[2] += size
  end

  a = h.to_a
  a.each { |k, v| v << v[2] / v[0] }

  a.sort { |x, y| x.last[1] <=> y.last[1] }.reverse
end

.psObject

Very naive : does a “ps aux | grep pid”.

Returns a hash like this one :

{
  :user => "jmettraux",
  :pid => "1100",
  :cpu => "73.0",
  :mem => "0.8",
  :vsz => "2472732",
  :rss => "35308",
  :tt => "s001",
  :stat => "S+",
  :started => "8:55PM",
  :time => "0:01.05",
  :command => "ruby start.rb"
}


113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruote/util/look.rb', line 113

def self.ps

  s = `ps aux | egrep '^.+ +#{$$} '`.split(' ')
  s[10] = s[10..-1].join(' ') # the command

  h = {}

  %w[
    user pid cpu mem vsz rss tt stat started time command
  ].each_with_index { |k, i| h[k.to_sym] = s[i] }

  h
end