From e626da82eae3d437b84d4f9ead0164d436b08e1a Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 29 Feb 2024 13:17:22 +0100 Subject: Don't pin named structs defined in Ruby [Bug #20311] `rb_define_class_under` assumes it's called from C and that the reference might be held in a C global variable, so it adds the class to the VM root. In the case of `Struct.new('Name')` it's wasteful and make the struct immortal. --- test/ruby/test_struct.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index ed750b91f7..a451e400cb 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -534,6 +534,20 @@ module TestStruct assert_equal [[:req, :_]], klass.instance_method(:c=).parameters end + def test_named_structs_are_not_rooted + # [Bug #20311] + assert_no_memory_leak([], <<~PREP, <<~CODE, rss: true) + code = proc do + Struct.new("A") + Struct.send(:remove_const, :A) + end + + 1_000.times(&code) + PREP + 300_000.times(&code) + CODE + end + class TopStruct < Test::Unit::TestCase include TestStruct -- cgit v1.2.3