def analyze
@result = MemoryInfo.new roots, 0, 0, 0
@objs = {}
queue = roots.dup
until queue.empty?
obj = queue.shift
case obj
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