From 67b2c21c327c96d80b8a0fe02a96d417e85293e8 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 2 Nov 2020 14:40:29 -0800 Subject: Add `GC.auto_compact= true/false` and `GC.auto_compact` * `GC.auto_compact=`, `GC.auto_compact` can be used to control when compaction runs. Setting `auto_compact=` to true will cause compaction to occurr duing major collections. At the moment, compaction adds significant overhead to major collections, so please test first! [Feature #17176] --- test/ruby/test_gc_compact.rb | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_gc_compact.rb b/test/ruby/test_gc_compact.rb index 75d9b01f2c..0ceb54dba7 100644 --- a/test/ruby/test_gc_compact.rb +++ b/test/ruby/test_gc_compact.rb @@ -3,6 +3,71 @@ require 'test/unit' require 'fiddle' class TestGCCompact < Test::Unit::TestCase + def test_enable_autocompact + before = GC.auto_compact + GC.auto_compact = true + assert GC.auto_compact + ensure + GC.auto_compact = before + end + + def test_disable_autocompact + before = GC.auto_compact + GC.auto_compact = false + refute GC.auto_compact + ensure + GC.auto_compact = before + end + + def test_major_compacts + before = GC.auto_compact + GC.auto_compact = true + compact = GC.stat :compact_count + GC.start + assert_operator GC.stat(:compact_count), :>, compact + ensure + GC.auto_compact = before + end + + def test_implicit_compaction_does_something + before = GC.auto_compact + list = [] + list2 = [] + + # Try to make some fragmentation + 500.times { + list << Object.new + Object.new + Object.new + } + count = GC.stat :compact_count + GC.auto_compact = true + loop do + break if count < GC.stat(:compact_count) + list2 << Object.new + end + compact_stats = GC.latest_compact_info + refute_predicate compact_stats[:considered], :empty? + refute_predicate compact_stats[:moved], :empty? + ensure + GC.auto_compact = before + end + + def test_gc_compact_stats + list = [] + list2 = [] + + # Try to make some fragmentation + 500.times { + list << Object.new + Object.new + Object.new + } + compact_stats = GC.compact + refute_predicate compact_stats[:considered], :empty? + refute_predicate compact_stats[:moved], :empty? + end + def memory_location(obj) (Fiddle.dlwrap(obj) >> 1) end -- cgit v1.2.3