From 417b1a36447cb2c650de55b433ba623541fb8bb3 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Mon, 27 Mar 2023 13:55:43 -0400 Subject: Fix memory leak for iclass [Bug #19550] If !RCLASS_EXT_EMBEDDED (e.g. 32 bit systems) then the rb_classext_t is allocated throug malloc so it must be freed. The issue can be seen in the following script: ``` 20.times do 100_000.times do mod = Module.new Class.new do include mod end end # Output the Resident Set Size (memory usage, in KB) of the current Ruby process puts `ps -o rss= -p #{$$}` end ``` Before this fix, the max RSS is 280MB, while after this change, it's 30MB. --- test/ruby/test_module.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index f566eced3a..b915a6ee8f 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -3276,6 +3276,21 @@ class TestModule < Test::Unit::TestCase assert_match(/::Foo$/, mod.name, '[Bug #14895]') end + def test_iclass_memory_leak + # [Bug #19550] + assert_no_memory_leak([], <<~PREP, <<~CODE, rss: true) + code = proc do + mod = Module.new + Class.new do + include mod + end + end + 1_000.times(&code) + PREP + 3_000_000.times(&code) + CODE + end + private def assert_top_method_is_private(method) -- cgit v1.2.3