Class: Memory::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/measure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



24
25
26
# File 'lib/measure.rb', line 24

def result
  @result
end

#rootsObject

Returns the value of attribute roots.



23
24
25
# File 'lib/measure.rb', line 23

def roots
  @roots
end

Instance Method Details

#analyzeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/measure.rb', line 26

def analyze
  @result = MemoryInfo.new roots, 0, 0, 0
  @objs = {}

  queue = roots.dup

  until queue.empty?
    obj = queue.shift

    case obj
      # special treatment for some types
      # some are certainly missing from this
    when IO
      visit(obj)
    when String
      visit(obj) { @result.bytes += obj.size }
    when Fixnum
      @result.bytes += FIXNUM_SIZE
    when Array
      visit(obj) do
        @result.bytes += obj.size * REF_SIZE
        queue.concat(obj)
      end
    when Hash
      visit(obj) do
        @result.bytes += obj.size * REF_SIZE * 2
        obj.each {|k,v| queue.push(k).push(v)}
      end
    when Enumerable
      visit(obj) do
        obj.each do |o|
          @result.bytes += REF_SIZE
          queue.push(o)
        end
      end
    else
      visit(obj) do
        obj.instance_variables.each do |var|
          @result.bytes += REF_SIZE
          queue.push(obj.instance_variable_get(var))
        end
      end
    end
  end

  @result
end