Method: ObjectSpace.trace_object_allocations

Defined in:
object_tracing.c

.trace_object_allocations { ... } ⇒ Object

Starts tracing object allocations from the ObjectSpace extension module.

For example:

require ‘objspace’

class C

include ObjectSpace

def foo
  trace_object_allocations do
    obj = Object.new
    p "#{allocation_sourcefile(obj)}:#{allocation_sourceline(obj)}"
  end
end

end

C.new.foo #=> “objtrace.rb:8”

This example has included the ObjectSpace module to make it easier to read, but you can also use the ::trace_object_allocations notation (recommended).

Note that this feature introduces a huge performance decrease and huge memory consumption.

Yields:



268
269
270
271
272
273
# File 'object_tracing.c', line 268

static VALUE
trace_object_allocations(VALUE self)
{
    trace_object_allocations_start(self);
    return rb_ensure(rb_yield, Qnil, trace_object_allocations_stop, self);
}