From: "tmm1 (Aman Gupta)" Date: 2013-03-18T23:16:21+09:00 Subject: [ruby-core:53505] [ruby-trunk - Bug #8092] [patch] gc: improve accuracy of objspace_live_num() and allocated/freed counters Issue #8092 has been updated by tmm1 (Aman Gupta). With this updated patch the test is passing both before and after r39811. Problem was in finalize_list. After r39811 removing heap is less common, so the failure did not appear. diff --git a/gc.c b/gc.c index 2afd311..e72b198 100644 --- a/gc.c +++ b/gc.c @@ -1431,12 +1431,10 @@ finalize_list(rb_objspace_t *objspace, RVALUE *p) while (p) { RVALUE *tmp = p->as.free.next; run_final(objspace, (VALUE)p); + objspace->total_freed_object_num++; if (!FL_TEST(p, FL_SINGLETON)) { /* not freeing page */ add_slot_local_freelist(objspace, p); - if (!is_lazy_sweeping(objspace)) { - objspace->total_freed_object_num++; - objspace->heap.free_num++; - } + objspace->heap.free_num++; } else { struct heaps_slot *slot = (struct heaps_slot *)(VALUE)RDATA(p)->dmark; @@ -1940,9 +1938,9 @@ slot_sweep(rb_objspace_t *objspace, struct heaps_slot *sweep_slot) else { sweep_slot->free_next = NULL; } - objspace->total_freed_object_num += freed_num; objspace->heap.free_num += freed_num + empty_num; } + objspace->total_freed_object_num += freed_num; objspace->heap.final_num += final_num; if (deferred_final_list && !finalizing) { @@ -2969,11 +2967,11 @@ rb_gc_force_recycle(VALUE p) rb_objspace_t *objspace = &rb_objspace; struct heaps_slot *slot; + objspace->total_freed_object_num++; if (MARKED_IN_BITMAP(GET_HEAP_BITMAP(p), p)) { add_slot_local_freelist(objspace, (RVALUE *)p); } else { - objspace->total_freed_object_num++; objspace->heap.free_num++; slot = add_slot_local_freelist(objspace, (RVALUE *)p); if (slot->free_next == NULL) { diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb index 90c4787..b1e52fc 100644 --- a/test/ruby/test_gc.rb +++ b/test/ruby/test_gc.rb @@ -64,6 +64,12 @@ class TestGc < Test::Unit::TestCase assert_equal(arg, res) assert_equal(false, res.empty?) assert_kind_of(Integer, res[:count]) + + stat, count = {}, {} + GC.start + GC.stat(stat) + ObjectSpace.count_objects(count) + assert_equal(count[:TOTAL]-count[:FREE], stat[:heap_live_num]) end def test_singleton_method ---------------------------------------- Bug #8092: [patch] gc: improve accuracy of objspace_live_num() and allocated/freed counters https://bugs.ruby-lang.org/issues/8092#change-37697 Author: tmm1 (Aman Gupta) Status: Open Priority: Normal Assignee: authorNari (Narihiro Nakamura) Category: core Target version: ruby -v: ruby 2.1.0dev (2013-03-14 trunk 39748) [x86_64-darwin12.2.1] Test with large rails app: ruby -e' require "./config/environment" stat, count = {}, {} GC.start GC.stat(stat) ObjectSpace.count_objects(count) printf "%d == %d\n", stat[:heap_live_num], count[:TOTAL]-count[:FREE] ' Without patch: 632974 == 628506 With patch: 628506 == 628506 diff --git a/gc.c b/gc.c index bd95073..48f9470 100644 --- a/gc.c +++ b/gc.c @@ -1432,10 +1432,8 @@ finalize_list(rb_objspace_t *objspace, RVALUE *p) run_final(objspace, (VALUE)p); if (!FL_TEST(p, FL_SINGLETON)) { /* not freeing page */ add_slot_local_freelist(objspace, p); - if (!is_lazy_sweeping(objspace)) { - objspace->total_freed_object_num++; - objspace->heap.free_num++; - } + objspace->total_freed_object_num++; + objspace->heap.free_num++; } else { struct heaps_slot *slot = (struct heaps_slot *)(VALUE)RDATA(p)->dmark; @@ -1939,9 +1937,9 @@ slot_sweep(rb_objspace_t *objspace, struct heaps_slot *sweep_slot) else { sweep_slot->free_next = NULL; } - objspace->total_freed_object_num += freed_num; objspace->heap.free_num += freed_num + empty_num; } + objspace->total_freed_object_num += freed_num; objspace->heap.final_num += final_num; if (deferred_final_list && !finalizing) { @@ -2965,11 +2963,11 @@ rb_gc_force_recycle(VALUE p) rb_objspace_t *objspace = &rb_objspace; struct heaps_slot *slot; + objspace->total_freed_object_num++; if (MARKED_IN_BITMAP(GET_HEAP_BITMAP(p), p)) { add_slot_local_freelist(objspace, (RVALUE *)p); } else { - objspace->total_freed_object_num++; objspace->heap.free_num++; slot = add_slot_local_freelist(objspace, (RVALUE *)p); if (slot->free_next == NULL) { -- http://bugs.ruby-lang.org/