Project

General

Profile

Actions

Feature #17762

closed

A simple way to trace object allocation

Added by mame (Yusuke Endoh) almost 3 years ago. Updated almost 3 years ago.

Status:
Closed
Target version:
-
[ruby-core:103102]

Description

How about having a short hand to ObjectSpace.trace_object_allocations_start, ObjectSpace.allocation_sourcefile and ObjectSpace.allocation_sourceline?

They are a very powerful tool for debugging and code-reading which allows us to identify an allocation site of an object.
Though they are never lightweight, they are the last resort when you try debugging code written by someone else.

However, the names are too long for me to remember and to type. Whenever I want to use them, I have to google, copy and paste the names.

Proposal

To enable trace allocations:

require "objspace/trace" #=> objspace/trace is enabled

To show the allocation site of an object:

p obj #=> #<Object:0x...> @ (file.rb):(lineno)

Example

require "objspace/trace"
require "active_support/all"

p ActiveSupport::VERSION::STRING
  #=> "6.1.3.1" @ /home/mame/work/ruby/local/lib/ruby/gems/3.1.0/gems/activesupport-6.1.3.1/lib/active_support/gem_version.rb:15

Discussion

I've attached a simple patch that is originally authored by @ko1 (Koichi Sasada) .

  • Is the message objspace/trace is enabled needed or not?
  • To stop the trace, you need to use ObjectSpace.trace_object_allocations_stop. But, I guess that it is rare that we need to stop it during debugging.
  • Is it too radical to redefine Kernel#p? I think that it is good enough for many cases. When it matters, the original APIs (ObjectSpace.trace_object_allocations_start, ...) can be used.

Files

objspace-trace.patch (631 Bytes) objspace-trace.patch mame (Yusuke Endoh), 03/30/2021 02:49 PM

Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #10932: Enabling allocation tracing as early as possibleOpenko1 (Koichi Sasada)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0