Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
ObjectSpace.count_symbols
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;
}
Returns a hash containing the number of objects for each Symbol type.
The types of Symbols are the following:
-
mortal_dynamic_symbol: Symbols that are garbage collectable. -
immortal_dynamic_symbol: Symbols that are objects allocated from the garbage collector, but are not garbage collectable. -
immortal_static_symbol: Symbols that are not allocated from the garbage collector, and are thus not garbage collectable. -
immortal_symbol: the sum ofimmortal_dynamic_symbolandimmortal_static_symbol.
If the optional argument result_hash is given, it is overwritten and returned. This is intended to avoid the probe effect.
This method is intended for developers interested in performance and memory usage of Ruby programs. The contents of the returned hash is implementation specific and may change in the future.
This method is only expected to work with C Ruby.