Module: MemoryProfiler::ObjectSpaceAnalyser

Defined in:
lib/memory-profiler.rb

Class Method Summary collapse

Class Method Details

.__save(str) ⇒ Object

a single place where the magic filename is defined



308
309
310
311
312
313
# File 'lib/memory-profiler.rb', line 308

def self.__save(str) #:nodoc:
        File.open("/tmp/memory_profiler-#{Process.pid}-strings-#{Time.now.to_i}.log", 'w') do |f|
                str.sort.each{|s| f.puts s }
        end
        str = nil
end

.__sizeof(o) ⇒ Object

Estimates the size of an object using Marshall.dump() Defaults to 1 if anything goes wrong.



301
302
303
304
305
# File 'lib/memory-profiler.rb', line 301

def self.__sizeof(o) #:nodoc:
        Marshall.dump(o).size
rescue ::Exception
        1
end

.analyse(opt = {}) ⇒ Object

Returns a hash mapping each Class to its usage.

If opt is true, the usage is estimated using Marshall.dump() for each instance; otherwise it is a simple instance count.

If opt is true, the analyser writes a text file containing every string in the Ruby ObjectSpace, at: /tmp/memory_profiler-<pid>-strings-<time>.log

Uses opt and opt , as per MemoryProfiler#start



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/memory-profiler.rb', line 277

def self.analyse(opt = {})
        opt = MemoryProfiler::DEFAULTS.merge(opt)
        marshall_size = !!opt[:marshall_size]
        string_debug = !!opt[:string_debug]
        ign  = opt[:ignore]
        only = opt[:only]

        res = Hash.new(0)
        str = [] if string_debug
        ObjectSpace.each_object do |o|
                if res[o.class] or ((only.empty? or only.any?{|y| o.is_a? y }) and ign.none?{|x| o.is_a? x })
                        res[o.class] += (marshall_size ? self.__sizeof(o) : 1)
                end
                str.push o.inspect if string_debug and o.class == String
        end
        if string_debug
                self.__save str
                str = nil
        end
        res
end