Module: ObjectSpace

Defined in:
ext/objspace/objspace.c,
ext/objspace/objspace.c,
ext/objspace/lib/objspace.rb,
ext/objspace/objspace_dump.c,
ext/objspace/object_tracing.c

Overview

The objspace library extends the ObjectSpace module and adds several methods to get internal statistic information about object/memory management.

You need to require 'objspace' to use this extension module.

Generally, you SHOULD NOT use this library if you do not know about the MRI implementation. Mainly, this library is for (memory) profiler developers and MRI developers who need to know about MRI memory usage.

Defined Under Namespace

Classes: InternalObjectWrapper

Class Method Summary collapse

Class Method Details

._dump(obj, output) ⇒ Object

:nodoc:



741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'ext/objspace/objspace_dump.c', line 741

static VALUE
objspace_dump(VALUE os, VALUE obj, VALUE output)
{
    struct dump_config dc = {0,};
    if (!RB_SPECIAL_CONST_P(obj)) {
        dc.cur_page_slot_size = rb_gc_obj_slot_size(obj);
    }

    dump_output(&dc, output, Qnil, Qnil, Qnil);

    dump_object(obj, &dc);

    return dump_result(&dc);
}

._dump_all(output, full, since, shapes) ⇒ Object

:nodoc:



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'ext/objspace/objspace_dump.c', line 815

static VALUE
objspace_dump_all(VALUE os, VALUE output, VALUE full, VALUE since, VALUE shapes)
{
    struct dump_config dc = {0,};
    dump_output(&dc, output, full, since, shapes);

    if (!dc.partial_dump || dc.since == 0) {
        /* dump roots */
        rb_objspace_reachable_objects_from_root(root_obj_i, &dc);
        if (dc.roots) dump_append(&dc, "]}\n");
    }

    if (RTEST(shapes)) {
        rb_shape_each_shape(shape_i, &dc);
    }

    /* dump all objects */
    rb_objspace_each_objects(heap_i, &dc);

    return dump_result(&dc);
}

._dump_shapes(output, shapes) ⇒ Object

:nodoc:



838
839
840
841
842
843
844
845
846
847
848
# File 'ext/objspace/objspace_dump.c', line 838

static VALUE
objspace_dump_shapes(VALUE os, VALUE output, VALUE shapes)
{
    struct dump_config dc = {0,};
    dump_output(&dc, output, Qfalse, Qnil, shapes);

    if (RTEST(shapes)) {
        rb_shape_each_shape(shape_i, &dc);
    }
    return dump_result(&dc);
}

.allocation_class_path(object) ⇒ String

Returns the class for the given object.

class A

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

end

A.new.foo #=> “Class”

See ::trace_object_allocations for more information and examples.

Returns:



490
491
492
493
494
495
496
497
498
499
500
501
# File 'ext/objspace/object_tracing.c', line 490

static VALUE
allocation_class_path(VALUE self, VALUE obj)
{
    struct allocation_info *info = lookup_allocation_info(obj);

    if (info && info->class_path) {
        return rb_str_new2(info->class_path);
    }
    else {
        return Qnil;
    }
}

.allocation_generation(object) ⇒ Integer?

Returns garbage collector generation for the given object.

class B

include ObjectSpace

def foo
  trace_object_allocations do
    obj = Object.new
    p "Generation is #{allocation_generation(obj)}"
  end
end

end

B.new.foo #=> “Generation is 3”

See ::trace_object_allocations for more information and examples.

Returns:



555
556
557
558
559
560
561
562
563
564
565
# File 'ext/objspace/object_tracing.c', line 555

static VALUE
allocation_generation(VALUE self, VALUE obj)
{
    struct allocation_info *info = lookup_allocation_info(obj);
    if (info) {
        return SIZET2NUM(info->generation);
    }
    else {
        return Qnil;
    }
}

.allocation_method_id(object) ⇒ String

Returns the method identifier for the given object.

class A

include ObjectSpace

def foo
  trace_object_allocations do
    obj = Object.new
    p "#{allocation_class_path(obj)}##{allocation_method_id(obj)}"
  end
end

end

A.new.foo #=> “Class#new”

See ::trace_object_allocations for more information and examples.

Returns:



523
524
525
526
527
528
529
530
531
532
533
# File 'ext/objspace/object_tracing.c', line 523

static VALUE
allocation_method_id(VALUE self, VALUE obj)
{
    struct allocation_info *info = lookup_allocation_info(obj);
    if (info) {
        return info->mid;
    }
    else {
        return Qnil;
    }
}

.allocation_sourcefile(object) ⇒ String

Returns the source file origin from the given object.

See ::trace_object_allocations for more information and examples.

Returns:



439
440
441
442
443
444
445
446
447
448
449
450
# File 'ext/objspace/object_tracing.c', line 439

static VALUE
allocation_sourcefile(VALUE self, VALUE obj)
{
    struct allocation_info *info = lookup_allocation_info(obj);

    if (info && info->path) {
        return rb_str_new2(info->path);
    }
    else {
        return Qnil;
    }
}

.allocation_sourceline(object) ⇒ Integer

Returns the original line from source for from the given object.

See ::trace_object_allocations for more information and examples.

Returns:



459
460
461
462
463
464
465
466
467
468
469
470
# File 'ext/objspace/object_tracing.c', line 459

static VALUE
allocation_sourceline(VALUE self, VALUE obj)
{
    struct allocation_info *info = lookup_allocation_info(obj);

    if (info) {
        return INT2FIX(info->line);
    }
    else {
        return Qnil;
    }
}

.count_imemo_objects([result_hash]) ⇒ Hash

Counts objects for each T_IMEMO type.

This method is only for MRI developers interested in performance and memory usage of Ruby programs.

It returns a hash as:

{:imemo_ifunc=>8,
 :imemo_svar=>7,
 :imemo_cref=>509,
 :imemo_memo=>1,
 :imemo_throw_data=>1}

If the optional argument, result_hash, is given, it is overwritten and returned. This is intended to avoid probe effect.

The contents of the returned hash is implementation specific and may change in the future.

In this version, keys are symbol objects.

This method is only expected to work with C Ruby.

Returns:



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'ext/objspace/objspace.c', line 487

static VALUE
count_imemo_objects(int argc, VALUE *argv, VALUE self)
{
    VALUE hash = setup_hash(argc, argv);

    if (imemo_type_ids[0] == 0) {
#define INIT_IMEMO_TYPE_ID(n) (imemo_type_ids[n] = rb_intern_const(#n))
        INIT_IMEMO_TYPE_ID(imemo_env);
        INIT_IMEMO_TYPE_ID(imemo_cref);
        INIT_IMEMO_TYPE_ID(imemo_svar);
        INIT_IMEMO_TYPE_ID(imemo_throw_data);
        INIT_IMEMO_TYPE_ID(imemo_ifunc);
        INIT_IMEMO_TYPE_ID(imemo_memo);
        INIT_IMEMO_TYPE_ID(imemo_ment);
        INIT_IMEMO_TYPE_ID(imemo_iseq);
        INIT_IMEMO_TYPE_ID(imemo_tmpbuf);
        INIT_IMEMO_TYPE_ID(imemo_ast);
        INIT_IMEMO_TYPE_ID(imemo_parser_strterm);
        INIT_IMEMO_TYPE_ID(imemo_callinfo);
        INIT_IMEMO_TYPE_ID(imemo_callcache);
        INIT_IMEMO_TYPE_ID(imemo_constcache);
#undef INIT_IMEMO_TYPE_ID
    }

    each_object_with_flags(count_imemo_objects_i, (void *)hash);

    return hash;
}

.count_nodes([result_hash]) ⇒ Hash

Counts nodes for each node type.

This method is only for MRI developers interested in performance and memory usage of Ruby programs.

It returns a hash as:

:NODE_FBODY=>1927, :NODE_CFUNC=>1798, …

If the optional argument, result_hash, is given, it is overwritten and returned. This is intended to avoid probe effect.

Note: The contents of the returned hash is implementation defined. It may be changed in future.

This method is only expected to work with C Ruby.

Returns:



362
363
364
365
366
# File 'ext/objspace/objspace.c', line 362

static VALUE
count_nodes(int argc, VALUE *argv, VALUE os)
{
    return setup_hash(argc, argv);
}

.count_objects_size([result_hash]) ⇒ Hash

Counts objects size (in bytes) for each type.

Note that this information is incomplete. You need to deal with this information as only a HINT. Especially, total size of T_DATA may be wrong.

It returns a hash as:

{:TOTAL=>1461154, :T_CLASS=>158280, :T_MODULE=>20672, :T_STRING=>527249, ...}

If the optional argument, result_hash, is given, it is overwritten and returned. This is intended to avoid probe effect.

The contents of the returned hash is implementation defined. It may be changed in future.

This method is only expected to work with C Ruby.

Returns:



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'ext/objspace/objspace.c', line 248

static VALUE
count_objects_size(int argc, VALUE *argv, VALUE os)
{
    size_t counts[T_MASK+1];
    size_t total = 0;
    enum ruby_value_type i;
    VALUE hash = setup_hash(argc, argv);

    for (i = 0; i <= T_MASK; i++) {
        counts[i] = 0;
    }

    each_object_with_flags(cos_i, &counts[0]);

    for (i = 0; i <= T_MASK; i++) {
        if (counts[i]) {
            VALUE type = type2sym(i);
            total += counts[i];
            rb_hash_aset(hash, type, SIZET2NUM(counts[i]));
        }
    }
    rb_hash_aset(hash, ID2SYM(rb_intern("TOTAL")), SIZET2NUM(total));
    return hash;
}

.count_symbols([result_hash]) ⇒ Hash

Counts symbols for each Symbol type.

This method is only for MRI developers interested in performance and memory usage of Ruby programs.

If the optional argument, result_hash, is given, it is overwritten and returned. This is intended to avoid probe effect.

Note: The contents of the returned hash is implementation defined. It may be changed in future.

This method is only expected to work with C Ruby.

On this version of MRI, they have 3 types of Symbols (and 1 total counts).

* mortal_dynamic_symbol: GC target symbols (collected by GC)
* immortal_dynamic_symbol: Immortal symbols promoted from dynamic symbols (do not collected by GC)
* immortal_static_symbol: Immortal symbols (do not collected by GC)
* immortal_symbol: total immortal symbols (immortal_dynamic_symbol+immortal_static_symbol)

Returns:



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'ext/objspace/objspace.c', line 322

static VALUE
count_symbols(int argc, VALUE *argv, VALUE os)
{
    struct dynamic_symbol_counts dynamic_counts = {0, 0};
    VALUE hash = setup_hash(argc, argv);

    size_t immortal_symbols = rb_sym_immortal_count();
    each_object_with_flags(cs_i, &dynamic_counts);

    rb_hash_aset(hash, ID2SYM(rb_intern("mortal_dynamic_symbol")),   SIZET2NUM(dynamic_counts.mortal));
    rb_hash_aset(hash, ID2SYM(rb_intern("immortal_dynamic_symbol")), SIZET2NUM(dynamic_counts.immortal));
    rb_hash_aset(hash, ID2SYM(rb_intern("immortal_static_symbol")),  SIZET2NUM(immortal_symbols - dynamic_counts.immortal));
    rb_hash_aset(hash, ID2SYM(rb_intern("immortal_symbol")),         SIZET2NUM(immortal_symbols));

    return hash;
}

.count_tdata_objects([result_hash]) ⇒ Hash

Counts objects for each T_DATA type.

This method is only for MRI developers interested in performance and memory usage of Ruby programs.

It returns a hash as:

:parser=>5, :barrier=>6, :mutex=>6, Proc=>60, RubyVM::Env=>57, Mutex=>1, Encoding=>99, ThreadGroup=>1, Binding=>1, Thread=>1, RubyVM=>1, :iseq=>1, Random=>1, ARGF.class=>1, Data=>1, :autoload=>3, Time=>2 # T_DATA objects existing at startup on r32276.

If the optional argument, result_hash, is given, it is overwritten and returned. This is intended to avoid probe effect.

The contents of the returned hash is implementation specific and may change in the future.

In this version, keys are Class object or Symbol object.

If object is kind of normal (accessible) object, the key is Class object. If object is not a kind of normal (internal) object, the key is symbol name, registered by rb_data_type_struct.

This method is only expected to work with C Ruby.

Returns:



427
428
429
430
431
432
433
# File 'ext/objspace/objspace.c', line 427

static VALUE
count_tdata_objects(int argc, VALUE *argv, VALUE self)
{
    VALUE hash = setup_hash(argc, argv);
    each_object_with_flags(cto_i, (void *)hash);
    return hash;
}

.dump(obj, output: :string) ⇒ Object

Dump the contents of a ruby object as JSON.

output can be one of: :stdout, :file, :string, or IO object.

  • :file means dumping to a tempfile and returning corresponding File object;

  • :stdout means printing the dump and returning nil;

  • :string means returning a string with the dump;

  • if an instance of IO object is provided, the output goes there, and the object is returned.

This method is only expected to work with C Ruby. This is an experimental method and is subject to change. In particular, the function signature and output format are not guaranteed to be compatible in future versions of ruby.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'ext/objspace/lib/objspace.rb', line 28

def dump(obj, output: :string)
  out = case output
  when :file, nil
    require 'tempfile'
    Tempfile.create(%w(rubyobj .json))
  when :stdout
    STDOUT
  when :string
    +''
  when IO
    output
  else
    raise ArgumentError, "wrong output option: #{output.inspect}"
  end

  ret = _dump(obj, out)
  return nil if output == :stdout
  ret
end

.dump_all(output: :file, full: false, since: nil, shapes: true) ⇒ Object

Dump the contents of the ruby heap as JSON.

output argument is the same as for #dump.

full must be a boolean. If true, all heap slots are dumped including the empty ones (T_NONE).

since must be a non-negative integer or nil.

If since is a positive integer, only objects of that generation and newer generations are dumped. The current generation can be accessed using GC::count. Objects that were allocated without object allocation tracing enabled are ignored. See ::trace_object_allocations for more information and examples.

If since is omitted or is nil, all objects are dumped.

shapes must be a boolean or a non-negative integer.

If shapes is a positive integer, only shapes newer than the provided shape id are dumped. The current shape_id can be accessed using RubyVM.stat(:next_shape_id).

If shapes is false, no shapes are dumped.

To only dump objects allocated past a certain point you can combine since and shapes:

ObjectSpace.trace_object_allocations
GC.start
gc_generation = GC.count
shape_generation = RubyVM.stat(:next_shape_id)
call_method_to_instrument
ObjectSpace.dump_all(since: gc_generation, shapes: shape_generation)

This method is only expected to work with C Ruby. This is an experimental method and is subject to change. In particular, the function signature and output format are not guaranteed to be compatible in future versions of ruby.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/objspace/lib/objspace.rb', line 84

def dump_all(output: :file, full: false, since: nil, shapes: true)
  out = case output
  when :file, nil
    require 'tempfile'
    Tempfile.create(%w(rubyheap .json))
  when :stdout
    STDOUT
  when :string
    +''
  when IO
    output
  else
    raise ArgumentError, "wrong output option: #{output.inspect}"
  end

  shapes = 0 if shapes == true
  ret = _dump_all(out, full, since, shapes)
  return nil if output == :stdout
  ret
end

.dump_shapes(output: :file, since: 0) ⇒ Object

Dump the contents of the ruby shape tree as JSON.

_output_ argument is the same as for #dump.

If _since_ is a positive integer, only shapes newer than the provided
shape id are dumped. The current shape_id can be accessed using <tt>RubyVM.stat(:next_shape_id)</tt>.

This method is only expected to work with C Ruby.
This is an experimental method and is subject to change.
In particular, the function signature and output format are
not guaranteed to be compatible in future versions of ruby.


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/objspace/lib/objspace.rb', line 116

def dump_shapes(output: :file, since: 0)
  out = case output
  when :file, nil
    require 'tempfile'
    Tempfile.create(%w(rubyshapes .json))
  when :stdout
    STDOUT
  when :string
    +''
  when IO
    output
  else
    raise ArgumentError, "wrong output option: #{output.inspect}"
  end

  ret = _dump_shapes(out, since)
  return nil if output == :stdout
  ret
end

.internal_class_of(obj) ⇒ Class, Module

MRI specific feature

Return internal class of obj.

obj can be an instance of InternalObjectWrapper.

Note that you should not use this method in your application.

Returns:



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'ext/objspace/objspace.c', line 756

static VALUE
objspace_internal_class_of(VALUE self, VALUE obj)
{
    VALUE klass;

    if (rb_typeddata_is_kind_of(obj, &iow_data_type)) {
        obj = (VALUE)DATA_PTR(obj);
    }

    if (RB_TYPE_P(obj, T_IMEMO)) {
        return Qnil;
    }
    else {
        klass = CLASS_OF(obj);
        return wrap_klass_iow(klass);
    }
}

.internal_super_of(cls) ⇒ Class, Module

MRI specific feature

Return internal super class of cls (Class or Module).

obj can be an instance of InternalObjectWrapper.

Note that you should not use this method in your application.

Returns:



783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
# File 'ext/objspace/objspace.c', line 783

static VALUE
objspace_internal_super_of(VALUE self, VALUE obj)
{
    VALUE super;

    if (rb_typeddata_is_kind_of(obj, &iow_data_type)) {
        obj = (VALUE)DATA_PTR(obj);
    }

    switch (OBJ_BUILTIN_TYPE(obj)) {
      case T_MODULE:
      case T_CLASS:
      case T_ICLASS:
        super = RCLASS_SUPER(obj);
        break;
      default:
        rb_raise(rb_eArgError, "class or module is expected");
    }

    return wrap_klass_iow(super);
}

.memsize_of(obj) ⇒ Integer

Return consuming memory size of obj in bytes.

Note that the return size is incomplete. You need to deal with this information as only a HINT. Especially, the size of T_DATA may not be correct.

This method is only expected to work with C Ruby.

From Ruby 2.2, memsize_of(obj) returns a memory size includes sizeof(RVALUE).

Returns:



47
48
49
50
51
# File 'ext/objspace/objspace.c', line 47

static VALUE
memsize_of_m(VALUE self, VALUE obj)
{
    return SIZET2NUM(rb_obj_memsize_of(obj));
}

.memsize_of_all([klass]) ⇒ Integer

Return consuming memory size of all living objects in bytes.

If klass (should be Class object) is given, return the total memory size of instances of the given class.

Note that the returned size is incomplete. You need to deal with this information as only a HINT. Especially, the size of T_DATA may not be correct.

Note that this method does NOT return total malloc’ed memory size.

This method can be defined by the following Ruby code:

def memsize_of_all klass = false total = 0 ObjectSpace.each_object{|e| total += ObjectSpace.memsize_of(e) if klass == false || e.kind_of?(klass) } total end

This method is only expected to work with C Ruby.

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
# File 'ext/objspace/objspace.c', line 136

static VALUE
memsize_of_all_m(int argc, VALUE *argv, VALUE self)
{
    struct total_data data = {0, 0};

    if (argc > 0) {
        rb_scan_args(argc, argv, "01", &data.klass);
    }

    each_object_with_flags(total_i, &data);
    return SIZET2NUM(data.total);
}

.reachable_objects_from(obj) ⇒ Array?

MRI specific feature

Return all reachable objects from ‘obj’.

This method returns all reachable objects from ‘obj’.

If ‘obj’ has two or more references to the same object ‘x’, then returned array only includes one ‘x’ object.

If ‘obj’ is a non-markable (non-heap management) object such as true, false, nil, symbols and Fixnums (and Flonum) then it simply returns nil.

If ‘obj’ has references to an internal object, then it returns instances of ObjectSpace::InternalObjectWrapper class. This object contains a reference to an internal object and you can check the type of internal object with ‘type’ method.

If ‘obj’ is instance of ObjectSpace::InternalObjectWrapper class, then this method returns all reachable object from an internal object, which is pointed by ‘obj’.

With this method, you can find memory leaks.

This method is only expected to work except with C Ruby.

Example:

ObjectSpace.reachable_objects_from(['a', 'b', 'c'])
#=> [Array, 'a', 'b', 'c']

ObjectSpace.reachable_objects_from(['a', 'a', 'a'])
#=> [Array, 'a', 'a', 'a'] # all 'a' strings have different object id

ObjectSpace.reachable_objects_from([v = 'a', v, v])
#=> [Array, 'a']

ObjectSpace.reachable_objects_from(1)
#=> nil # 1 is not markable (heap managed) object

Returns:



644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'ext/objspace/objspace.c', line 644

static VALUE
reachable_objects_from(VALUE self, VALUE obj)
{
    if (rb_objspace_markable_object_p(obj)) {
        struct rof_data data;

        if (rb_typeddata_is_kind_of(obj, &iow_data_type)) {
            obj = (VALUE)DATA_PTR(obj);
        }

        data.refs = rb_obj_hide(rb_ident_hash_new());
        data.values = rb_ary_new();

        rb_objspace_reachable_objects_from(obj, reachable_object_from_i, &data);

        return data.values;
    }
    else {
        return Qnil;
    }
}

.reachable_objects_from_rootHash

MRI specific feature

Return all reachable objects from root.

Returns:



719
720
721
722
723
724
725
726
727
728
729
730
# File 'ext/objspace/objspace.c', line 719

static VALUE
reachable_objects_from_root(VALUE self)
{
    struct rofr_data data;
    VALUE hash = data.categories = rb_ident_hash_new();
    data.last_category = 0;

    rb_objspace_reachable_objects_from_root(reachable_object_from_root_i, &data);
    rb_hash_foreach(hash, collect_values_of_values, hash);

    return hash;
}

.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:



362
363
364
365
366
367
# File 'ext/objspace/object_tracing.c', line 362

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

.trace_object_allocations_clearObject

Clear recorded tracing information.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'ext/objspace/object_tracing.c', line 318

static VALUE
trace_object_allocations_clear(VALUE self)
{
    struct traceobj_arg *arg = get_traceobj_arg();

    /* clear tables */
    st_foreach(arg->object_table, free_values_i, 0);
    st_clear(arg->object_table);
    st_foreach(arg->str_table, free_keys_i, 0);
    st_clear(arg->str_table);

    /* do not touch TracePoints */

    return Qnil;
}

.trace_object_allocations_debug_startObject



402
403
404
405
406
407
408
409
410
411
412
# File 'ext/objspace/object_tracing.c', line 402

static VALUE
trace_object_allocations_debug_start(VALUE self)
{
    tmp_keep_remains = 1;
    if (object_allocations_reporter_registered == 0) {
        object_allocations_reporter_registered = 1;
        rb_bug_reporter_add(object_allocations_reporter, 0);
    }

    return trace_object_allocations_start(self);
}

.trace_object_allocations_startObject

Starts tracing object allocations.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'ext/objspace/object_tracing.c', line 262

static VALUE
trace_object_allocations_start(VALUE self)
{
    struct traceobj_arg *arg = get_traceobj_arg();

    if (arg->running++ > 0) {
        /* do nothing */
    }
    else {
        if (arg->newobj_trace == 0) {
            arg->newobj_trace = rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_NEWOBJ, newobj_i, arg);
            arg->freeobj_trace = rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_FREEOBJ, freeobj_i, arg);
        }
        rb_tracepoint_enable(arg->newobj_trace);
        rb_tracepoint_enable(arg->freeobj_trace);
    }

    return Qnil;
}

.trace_object_allocations_stopObject

Stop tracing object allocations.

Note that if ::trace_object_allocations_start is called n-times, then tracing will stop after calling ::trace_object_allocations_stop n-times.



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'ext/objspace/object_tracing.c', line 291

static VALUE
trace_object_allocations_stop(VALUE self)
{
    struct traceobj_arg *arg = get_traceobj_arg();

    if (arg->running > 0) {
        arg->running--;
    }

    if (arg->running == 0) {
        if (arg->newobj_trace != 0) {
            rb_tracepoint_disable(arg->newobj_trace);
        }
        if (arg->freeobj_trace != 0) {
            rb_tracepoint_disable(arg->freeobj_trace);
        }
    }

    return Qnil;
}