Module: ObjectSpace
- Defined in:
- gc.c
Overview
The ObjectSpace
module contains a number of routines that interact with the garbage collection facility and allow you to traverse all living objects with an iterator.
ObjectSpace
also provides support for object finalizers, procs that will be called when a specific object is about to be destroyed by garbage collection.
include ObjectSpace
a = "A"
b = "B"
c = "C"
define_finalizer(a, proc {|id| puts "Finalizer one on #{id}" })
define_finalizer(a, proc {|id| puts "Finalizer two on #{id}" })
define_finalizer(b, proc {|id| puts "Finalizer three on #{id}" })
produces:
Finalizer three on 537763470
Finalizer one on 537763480
Finalizer two on 537763480
Class Method Summary collapse
-
._id2ref(object_id) ⇒ Object
Converts an object id to a reference to the object.
-
.count_objects([result_hash]) ⇒ Hash
Counts objects for each type.
-
.define_finalizer(obj, aProc = proc()) ⇒ Object
Adds aProc as a finalizer, to be called after obj was destroyed.
-
.each_object ⇒ Object
Calls the block once for each living, nonimmediate object in this Ruby process.
-
.garbage_collect ⇒ Object
Initiates garbage collection, unless manually disabled.
-
.undefine_finalizer(obj) ⇒ Object
Removes all finalizers for obj.
Class Method Details
._id2ref(object_id) ⇒ Object
Converts an object id to a reference to the object. May not be called on an object id passed as a parameter to a finalizer.
s = "I am a string" #=> "I am a string"
r = ObjectSpace._id2ref(s.object_id) #=> "I am a string"
r == s #=> true
|
# File 'gc.c'
static VALUE
id2ref(VALUE obj, VALUE objid)
{
#if SIZEOF_LONG == SIZEOF_VOIDP
#define NUM2PTR(x) NUM2ULONG(x)
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
#define NUM2PTR(x) NUM2ULL(x)
#endif
rb_objspace_t *objspace = &rb_objspace;
VALUE ptr;
void *p0;
rb_secure(4);
ptr = NUM2PTR(objid);
p0 = (void *)ptr;
if (ptr == Qtrue) return Qtrue;
if (ptr == Qfalse) return Qfalse;
if (ptr == Qnil) return Qnil;
if (FIXNUM_P(ptr)) return (VALUE)ptr;
ptr = objid ^ FIXNUM_FLAG; /* unset FIXNUM_FLAG */
if ((ptr % sizeof(RVALUE)) == (4 << 2)) {
ID symid = ptr / sizeof(RVALUE);
if (rb_id2name(symid) == 0)
rb_raise(rb_eRangeError, "%p is not symbol id value", p0);
return ID2SYM(symid);
}
|
.count_objects([result_hash]) ⇒ Hash
Counts objects for each type.
It returns a hash as: :FREE=>3011, :T_OBJECT=>6, :T_CLASS=>404, ...
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 not expected to work except C Ruby.
|
# File 'gc.c'
static VALUE
count_objects(int argc, VALUE *argv, VALUE os)
{
rb_objspace_t *objspace = &rb_objspace;
size_t counts[T_MASK+1];
size_t freed = 0;
size_t total = 0;
size_t i;
VALUE hash;
if (rb_scan_args(argc, argv, "01", &hash) == 1) {
if (TYPE(hash) != T_HASH)
rb_raise(rb_eTypeError, "non-hash given");
}
|
.define_finalizer(obj, aProc = proc()) ⇒ Object
Adds aProc as a finalizer, to be called after obj was destroyed.
|
# File 'gc.c'
static VALUE
define_final(int argc, VALUE *argv, VALUE os)
{
rb_objspace_t *objspace = &rb_objspace;
VALUE obj, block, table;
st_data_t data;
rb_scan_args(argc, argv, "11", &obj, &block);
rb_check_frozen(obj);
if (argc == 1) {
block = rb_block_proc();
}
|
.each_object([) {|obj| ... } ⇒ Fixnum .each_object([) ⇒ Object
Calls the block once for each living, nonimmediate object in this Ruby process. If module is specified, calls the block for only those classes or modules that match (or are a subclass of) module. Returns the number of objects found. Immediate objects (Fixnum
s, Symbol
s true
, false
, and nil
) are never returned. In the example below, each_object
returns both the numbers we defined and several constants defined in the Math
module.
If no block is given, an enumerator is returned instead.
a = 102.7
b = 95 # Won't be returned
c = 12345678987654321
count = ObjectSpace.each_object(Numeric) {|x| p x }
puts "Total count: #{count}"
produces:
12345678987654321
102.7
2.71828182845905
3.14159265358979
2.22044604925031e-16
1.7976931348623157e+308
2.2250738585072e-308
Total count: 7
|
# File 'gc.c'
static VALUE
os_each_obj(int argc, VALUE *argv, VALUE os)
{
VALUE of;
rb_secure(4);
if (argc == 0) {
of = 0;
}
|
.start ⇒ nil .garbage_collect ⇒ nil .garbage_collect ⇒ nil
Initiates garbage collection, unless manually disabled.
|
# File 'gc.c'
VALUE
rb_gc_start(void)
{
rb_gc();
return Qnil;
}
|
.undefine_finalizer(obj) ⇒ Object
Removes all finalizers for obj.
|
# File 'gc.c'
static VALUE
undefine_final(VALUE os, VALUE obj)
{
rb_objspace_t *objspace = &rb_objspace;
st_data_t data = obj;
rb_check_frozen(obj);
st_delete(finalizer_table, &data, 0);
FL_UNSET(obj, FL_FINALIZE);
return obj;
}
|