summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Zhu <[email protected]>2023-03-27 13:55:43 -0400
committerPeter Zhu <[email protected]>2023-03-28 08:20:06 -0400
commit417b1a36447cb2c650de55b433ba623541fb8bb3 (patch)
treed364bf0939fb219ee6d00260c66d06d9b799a235
parent6ce6b4d1e7a6f6f20b232e8fc744c303bfd87098 (diff)
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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/7597
-rw-r--r--gc.c2
-rw-r--r--test/ruby/test_module.rb15
2 files changed, 16 insertions, 1 deletions
diff --git a/gc.c b/gc.c
index af789bc9ee..c03738986c 100644
--- a/gc.c
+++ b/gc.c
@@ -3677,7 +3677,7 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
cc_table_free(objspace, obj, FALSE);
rb_class_remove_from_module_subclasses(obj);
rb_class_remove_from_super_subclasses(obj);
-#if !USE_RVARGC
+#if !RCLASS_EXT_EMBEDDED
xfree(RCLASS_EXT(obj));
#endif
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)