diff options
| author | nagachika <nagachika@ruby-lang.org> | 2023-07-17 09:29:04 +0900 |
|---|---|---|
| committer | nagachika <nagachika@ruby-lang.org> | 2023-07-17 09:29:04 +0900 |
| commit | 9fb94407b97bb85ea344e67606f8ae9ba0bdbd48 (patch) | |
| tree | 74c8ae4ba3215c9660811f0b728150f9fb6ec373 | |
| parent | 45b14ef5c55e5b1956c590f4cfddacbccfc7bf8a (diff) | |
merge revision(s) 417b1a36447cb2c650de55b433ba623541fb8bb3: [Backport #19550]
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.
---
gc.c | 2 +-
test/ruby/test_module.rb | 15 +++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
| -rw-r--r-- | gc.c | 2 | ||||
| -rw-r--r-- | test/ruby/test_module.rb | 15 | ||||
| -rw-r--r-- | version.h | 2 |
3 files changed, 17 insertions, 2 deletions
@@ -3662,7 +3662,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 6c4defd6b1..54985f0f87 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -3274,6 +3274,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) @@ -11,7 +11,7 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 2 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 78 +#define RUBY_PATCHLEVEL 79 #include "ruby/version.h" #include "ruby/internal/abi.h" |
