Skip to content

Commit 40adb6c

Browse files
ko1tmm1
authored andcommitted
* gc.c: introduce new environment variable
"RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR" to control major/minor GC frequency. Do full GC when the number of old objects is more than R * N where R is this factor and N is the number of old objects just after last full GC. * test/ruby/test_gc.rb: add a test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45021 b2dd03c8-39d4-4d8f-98ff-823fe69b080e Conflicts: ChangeLog gc.c
1 parent 87c9373 commit 40adb6c

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

gc.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ rb_gc_guarded_ptr(volatile VALUE *ptr)
109109
#ifndef GC_HEAP_GROWTH_MAX_SLOTS
110110
#define GC_HEAP_GROWTH_MAX_SLOTS 0 /* 0 is disable */
111111
#endif
112+
#ifndef GC_HEAP_OLDOBJECT_LIMIT_FACTOR
113+
#define GC_HEAP_OLDOBJECT_LIMIT_FACTOR 2.0
114+
#endif
112115

113116
#ifndef GC_MALLOC_LIMIT_MIN
114117
#define GC_MALLOC_LIMIT_MIN (16 * 1024 * 1024 /* 16MB */)
@@ -135,6 +138,7 @@ typedef struct {
135138
unsigned int heap_free_slots;
136139
double growth_factor;
137140
unsigned int growth_max_slots;
141+
double oldobject_limit_factor;
138142
unsigned int malloc_limit_min;
139143
unsigned int malloc_limit_max;
140144
double malloc_limit_growth_factor;
@@ -151,6 +155,7 @@ static ruby_gc_params_t gc_params = {
151155
GC_HEAP_INIT_SLOTS,
152156
GC_HEAP_GROWTH_FACTOR,
153157
GC_HEAP_GROWTH_MAX_SLOTS,
158+
GC_HEAP_OLDOBJECT_LIMIT_FACTOR,
154159
GC_MALLOC_LIMIT_MIN,
155160
GC_MALLOC_LIMIT_MAX,
156161
GC_MALLOC_LIMIT_GROWTH_FACTOR,
@@ -4536,10 +4541,12 @@ gc_marks(rb_objspace_t *objspace, int full_mark)
45364541
#endif
45374542

45384543
gc_marks_body(objspace, TRUE);
4539-
4540-
/* Do full GC if old/remembered_shady object counts is greater than counts two times at last full GC counts */
4541-
objspace->rgengc.remembered_shady_object_limit = objspace->rgengc.remembered_shady_object_count * 2;
4542-
objspace->rgengc.old_object_limit = objspace->rgengc.old_object_count * 2;
4544+
{
4545+
/* See the comment about RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR */
4546+
const double r = gc_params.oldobject_limit_factor;
4547+
objspace->rgengc.remembered_shady_object_limit = objspace->rgengc.remembered_shady_object_count * r;
4548+
objspace->rgengc.old_object_limit = objspace->rgengc.old_object_count * r;
4549+
}
45434550
}
45444551
else { /* minor GC */
45454552
gc_marks_body(objspace, FALSE);
@@ -5744,6 +5751,10 @@ gc_set_initial_pages(void)
57445751
* - (next slots number) = (current slots number) * (this factor)
57455752
* * RUBY_GC_HEAP_GROWTH_MAX_SLOTS (new from 2.1)
57465753
* - Allocation rate is limited to this factor.
5754+
* * RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR (new from 2.1.1)
5755+
* - Do full GC when the number of old objects is more than R * N
5756+
* where R is this factor and
5757+
* N is the number of old objects just after last full GC.
57475758
*
57485759
* * obsolete
57495760
* * RUBY_FREE_MIN -> RUBY_GC_HEAP_FREE_SLOTS (from 2.1)
@@ -5782,6 +5793,7 @@ ruby_gc_set_params(int safe_level)
57825793

57835794
get_envparam_double("RUBY_GC_HEAP_GROWTH_FACTOR", &gc_params.growth_factor, 1.0);
57845795
get_envparam_int ("RUBY_GC_HEAP_GROWTH_MAX_SLOTS", &gc_params.growth_max_slots, 0);
5796+
get_envparam_double("RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR", &gc_params.oldobject_limit_factor, 0.0);
57855797

57865798
get_envparam_int("RUBY_GC_MALLOC_LIMIT", &gc_params.malloc_limit_min, 0);
57875799
get_envparam_int("RUBY_GC_MALLOC_LIMIT_MAX", &gc_params.malloc_limit_max, 0);

test/ruby/test_gc.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ def test_gc_parameter
177177
assert_in_out_err([env, "-w", "-e", "exit"], "", [], /RUBY_GC_HEAP_GROWTH_FACTOR=2.0/, "")
178178
assert_in_out_err([env, "-w", "-e", "exit"], "", [], /RUBY_GC_HEAP_GROWTH_MAX_SLOTS=10000/, "[ruby-core:57928]")
179179

180+
env = {
181+
"RUBY_GC_HEAP_INIT_SLOTS" => "100000",
182+
"RUBY_GC_HEAP_FREE_SLOTS" => "10000",
183+
"RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR" => "0.9",
184+
}
185+
assert_normal_exit("exit", "", :child_env => env)
186+
assert_in_out_err([env, "-w", "-e", "exit"], "", [], /RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR=0\.9/, "")
187+
# always full GC when RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR < 1.0
188+
assert_in_out_err([env, "-e", "1000_000.times{Object.new}; p(GC.stat[:minor_gc_count] < GC.stat[:major_gc_count])"], "", ['true'], [], "")
189+
180190
# check obsolete
181191
assert_in_out_err([{'RUBY_FREE_MIN' => '100'}, '-w', '-eexit'], '', [],
182192
/RUBY_FREE_MIN is obsolete. Use RUBY_GC_HEAP_FREE_SLOTS instead/)

0 commit comments

Comments
 (0)