From 1bc93a15912ab418bd54c0c064a47c557798eae9 Mon Sep 17 00:00:00 2001 From: nari Date: Mon, 18 Mar 2013 12:44:55 +0000 Subject: * gc.c: Avoid unnecessary heap growth. patched by tmm1(Aman Gupta). [Bug #8093] [ruby-core:53393] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39811 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ gc.c | 7 +++++-- test/ruby/test_gc.rb | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index eba63e5e28..0e508e22fa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Mar 18 21:42:48 2013 Narihiro Nakamura + + * gc.c: Avoid unnecessary heap growth. patched by tmm1(Aman Gupta). + [Bug #8093] [ruby-core:53393] + Mon Mar 18 17:58:36 2013 Narihiro Nakamura * gc.c: Fix unlimited memory growth with large values of diff --git a/gc.c b/gc.c index b568a39bd0..2afd3115a7 100644 --- a/gc.c +++ b/gc.c @@ -228,6 +228,7 @@ typedef struct rb_objspace { struct heaps_free_bitmap *free_bitmap; RVALUE *range[2]; struct heaps_header *freed; + size_t marked_num; size_t free_num; size_t free_min; size_t final_num; @@ -2001,7 +2002,7 @@ after_gc_sweep(rb_objspace_t *objspace) inc = ATOMIC_SIZE_EXCHANGE(malloc_increase, 0); if (inc > malloc_limit) { malloc_limit += - (size_t)((inc - malloc_limit) * (double)objspace_live_num(objspace) / (heaps_used * HEAP_OBJ_LIMIT)); + (size_t)((inc - malloc_limit) * (double)objspace->heap.marked_num / (heaps_used * HEAP_OBJ_LIMIT)); if (malloc_limit < initial_malloc_limit) malloc_limit = initial_malloc_limit; } @@ -2074,7 +2075,7 @@ gc_prepare_free_objects(rb_objspace_t *objspace) gc_marks(objspace); before_gc_sweep(objspace); - if (objspace->heap.free_min > (heaps_used * HEAP_OBJ_LIMIT - objspace_live_num(objspace))) { + if (objspace->heap.free_min > (heaps_used * HEAP_OBJ_LIMIT - objspace->heap.marked_num)) { set_heaps_increment(objspace); } @@ -2562,6 +2563,7 @@ gc_mark_ptr(rb_objspace_t *objspace, VALUE ptr) register uintptr_t *bits = GET_HEAP_BITMAP(ptr); if (MARKED_IN_BITMAP(bits, ptr)) return 0; MARK_IN_BITMAP(bits, ptr); + objspace->heap.marked_num++; return 1; } @@ -2922,6 +2924,7 @@ gc_marks(rb_objspace_t *objspace) objspace->mark_func_data = 0; gc_prof_mark_timer_start(objspace); + objspace->heap.marked_num = 0; objspace->count++; SET_STACK_END; diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb index bed58f3830..90c47875ee 100644 --- a/test/ruby/test_gc.rb +++ b/test/ruby/test_gc.rb @@ -146,4 +146,18 @@ class TestGc < Test::Unit::TestCase ObjectSpace.define_finalizer(Thread.main) { p 'finalize' } EOS end + + def test_expand_heap + assert_separately %w[--disable-gem], __FILE__, __LINE__, <<-'eom' + base_length = GC.stat[:heap_length] + (base_length * 500).times{ 'a' } + GC.start + assert_equal base_length, GC.stat[:heap_length], "invalid heap expanding" + + a = [] + (base_length * 500).times{ a << 'a' } + GC.start + assert base_length < GC.stat[:heap_length] + eom + end end -- cgit v1.2.3