class AllocationStats::Allocation

Information about an individual allocation is captured in this class.

Constants

ATTRIBUTES

a list of attributes that Allocation has on itself; inquiries in this list should just use Allocation's attributes, rather than the internal object's.

HELPERS

a list of helper methods that Allocation provides on top of the object that was allocated.

PWD

a convenience constants

Attributes

class_path[R]

@!attribute [r] class_path the classpath of where the object was allocated

line[R]

@!attribute [r] sourceline the line in the sourcefile where the object was allocated

memsize[RW]

@!attribute [rw] memsize the memsize of the object which was allocated

method_id[R]

@!attribute [r] method_id the method ID of where the object was allocated

object[R]

@!attribute [r] object the actual object that was allocated

sourceline[R]

@!attribute [r] sourceline the line in the sourcefile where the object was allocated

Public Class Methods

new(object) click to toggle source
# File lib/allocation_stats/allocation.rb, line 41
def initialize(object)
  @object = object
  @memsize    = ObjectSpace.memsize_of(object)
  @sourcefile = ObjectSpace.allocation_sourcefile(object)
  @sourceline = ObjectSpace.allocation_sourceline(object)
  @class_path = ObjectSpace.allocation_class_path(object)
  @method_id  = ObjectSpace.allocation_method_id(object)
end

Public Instance Methods

as_json() click to toggle source

Convert into a JSON string, which can be used in rack-allocation_stats's interactive mode.

# File lib/allocation_stats/allocation.rb, line 124
def as_json
  {
    "memsize"      => @memsize,
    "class_path"   => @class_path,
    "method_id"    => @method_id,
    "file"         => sourcefile_alias,
    "file (raw)"   => @sourcefile,
    "line"         => @sourceline,
    "class"        => @object.class.name,
    "class_plus"   => class_plus
  }
end
class_plus() click to toggle source

Returns class name, plus, for Arrays, extended information. When all of the elements of the Array are instances of a total of three or fewer classes, then those classes are listed in brackets. For example:

@example Array with only Fixnum and Bignum elements

allocation.class_plus  #=> "Array<Fixnum,Bignum>"

@example Array with elements of class A, B, C, and D

allocation.class_plus  #=> "Array"

@example String (not an Array)

allocation.class_plus  #=> "String"
# File lib/allocation_stats/allocation.rb, line 95
def class_plus
  case @object
  when Array
    object_classes = element_classes(@object.map {|e| e.class }.uniq)
    if object_classes
      "Array<#{object_classes}>"
    else
      "Array"
    end
  else
    @object.class.name
  end
end
file() click to toggle source

the sourcefile where the object was allocated

# File lib/allocation_stats/allocation.rb, line 51
def file; @sourcefile; end
gem() click to toggle source

Override Rubygems' Kernel#gem

@return [String] the name of the Rubygem where this allocation occurred. @return [nil] if this allocation did not occur in a Rubygem.

# File lib/allocation_stats/allocation.rb, line 113
def gem
  gem_regex = /#{AllocationStats::GEMDIR}#{File::SEPARATOR}
    gems#{File::SEPARATOR}
    (?<gem_name>[^#{File::SEPARATOR}]+)#{File::SEPARATOR}
  /x
  match = gem_regex.match(sourcefile)
  match && match[:gem_name]
end
sourcefile(alias_path = false) click to toggle source

Either the full source file (via `@sourcefile`), or the aliased source file, via {#sourcefile_alias}

@param [TrueClass] alias_path whether or not to alias the path

# File lib/allocation_stats/allocation.rb, line 81
def sourcefile(alias_path = false)
  alias_path ? sourcefile_alias : @sourcefile
end
sourcefile_alias() click to toggle source

If the source file has recognized paths in it, those portions of the full path will be aliased like so:

  • the present work directory is aliased to “<PWD>”

  • the Ruby lib directory (where the standard library lies) is aliased to “<RUBYLIBDIR>”

  • the Gem directory (where all gems lie) is aliased to “<GEMDIR>”

@return the source file, aliased.

# File lib/allocation_stats/allocation.rb, line 64
def sourcefile_alias
  case
  when @sourcefile[AllocationStats::RUBYLIBDIR]
    @sourcefile.sub(AllocationStats::RUBYLIBDIR, "<RUBYLIBDIR>")
  when @sourcefile[AllocationStats::GEMDIR]
    @sourcefile.sub(/#{AllocationStats::GEMDIR}\/gems\/([^\/]+)\//, '<GEM:\1>/')
  when @sourcefile[PWD]
    @sourcefile.sub(PWD, "<PWD>")
  else
    @sourcefile
  end
end
to_json(*a) click to toggle source

Convert into a JSON string, which can be used in rack-allocation_stats's interactive mode.

# File lib/allocation_stats/allocation.rb, line 139
def to_json(*a)
  as_json.to_json(*a)
end

Private Instance Methods

element_classes(classes) click to toggle source

@return either _the one_ class passed in, the two-to-four classes passed

in separated by commas, or `nil` if more than four classes were passed
in.

@api private

# File lib/allocation_stats/allocation.rb, line 148
def element_classes(classes)
  if classes.size == 1
    classes.first
  elsif classes.size > 1 && classes.size < 4
    classes.join(",")
  else
    nil
  end
end