Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
GC.latest_compact_info
static VALUE
gc_compact_stats(VALUE self)
{
rb_objspace_t *objspace = rb_gc_get_objspace();
VALUE h = rb_hash_new();
VALUE considered = rb_hash_new();
VALUE moved = rb_hash_new();
VALUE moved_up = rb_hash_new();
VALUE moved_down = rb_hash_new();
for (size_t i = 0; i < T_MASK; i++) {
if (objspace->rcompactor.considered_count_table[i]) {
rb_hash_aset(considered, type_sym(i), SIZET2NUM(objspace->rcompactor.considered_count_table[i]));
}
if (objspace->rcompactor.moved_count_table[i]) {
rb_hash_aset(moved, type_sym(i), SIZET2NUM(objspace->rcompactor.moved_count_table[i]));
}
if (objspace->rcompactor.moved_up_count_table[i]) {
rb_hash_aset(moved_up, type_sym(i), SIZET2NUM(objspace->rcompactor.moved_up_count_table[i]));
}
if (objspace->rcompactor.moved_down_count_table[i]) {
rb_hash_aset(moved_down, type_sym(i), SIZET2NUM(objspace->rcompactor.moved_down_count_table[i]));
}
}
rb_hash_aset(h, ID2SYM(rb_intern("considered")), considered);
rb_hash_aset(h, ID2SYM(rb_intern("moved")), moved);
rb_hash_aset(h, ID2SYM(rb_intern("moved_up")), moved_up);
rb_hash_aset(h, ID2SYM(rb_intern("moved_down")), moved_down);
return h;
}
Returns information about object moved in the most recent GC compaction.
The returned hash contains the following keys:
- considered
-
Hashcontaining the type of the object as the key and the number of objects of that type that were considered for movement. - moved
-
Hashcontaining the type of the object as the key and the number of objects of that type that were actually moved. - moved_up
-
Hashcontaining the type of the object as the key and the number of objects of that type that were increased in size. - moved_down
-
Hashcontaining the type of the object as the key and the number of objects of that type that were decreased in size.
Some objects can’t be moved (due to pinning) so these numbers can be used to calculate compaction efficiency.