Module: MemoryProfiler::ObjectSpaceAnalyser
- Defined in:
- lib/memory-profiler.rb
Class Method Summary collapse
-
.__save(str) ⇒ Object
a single place where the magic filename is defined.
-
.__sizeof(o) ⇒ Object
Estimates the size of an object using Marshal.dump() Defaults to 1 if anything goes wrong.
-
.analyse(opt = {}) ⇒ Object
Returns a hash mapping each Class to its usage.
Class Method Details
.__save(str) ⇒ Object
a single place where the magic filename is defined
313 314 315 316 317 318 |
# File 'lib/memory-profiler.rb', line 313 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 Marshal.dump() Defaults to 1 if anything goes wrong.
302 303 304 305 306 307 308 309 310 |
# File 'lib/memory-profiler.rb', line 302 def self.__sizeof(o) #:nodoc: if o.respond_to? :dump Marshal.dump(o).size else 1 end 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 Marshal.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
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/memory-profiler.rb', line 278 def self.analyse(opt = {}) opt = MemoryProfiler::DEFAULTS.merge(opt) marshal_size = !!(opt[:marshal_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] += (marshal_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 |