summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNARUSE, Yui <naruse@airemix.jp>2023-02-06 16:41:23 +0900
committerNARUSE, Yui <naruse@airemix.jp>2023-02-06 16:41:23 +0900
commit3426ebd0489654f951a8b92efaf5e72b9f43efab (patch)
tree8bc6c52d943692f6be09e85c5ba3387fdec5da68
parent3a88589399f7f1059be245f766809c49790ad939 (diff)
downloadruby-3426ebd0489654f951a8b92efaf5e72b9f43efab.tar.gz
merge revision(s) c6f84e918943a0bf8db6fee556fc53180d257510: [Backport #19398]
[Bug #19398] Memory leak in WeakMap There's a memory leak in ObjectSpace::WeakMap due to not freeing the `struct weakmap`. It can be seen in the following script: ``` 100.times do 10000.times do ObjectSpace::WeakMap.new end # Output the Resident Set Size (memory usage, in KB) of the current Ruby process puts `ps -o rss= -p #{$$}` end ``` --- gc.c | 1 + test/ruby/test_weakmap.rb | 9 +++++++++ 2 files changed, 10 insertions(+)
-rw-r--r--gc.c1
-rw-r--r--test/ruby/test_weakmap.rb9
-rw-r--r--version.h2
3 files changed, 11 insertions, 1 deletions
diff --git a/gc.c b/gc.c
index b30c022356..54400e4f6b 100644
--- a/gc.c
+++ b/gc.c
@@ -12843,6 +12843,7 @@ wmap_free(void *ptr)
st_foreach(w->obj2wmap, wmap_free_map, 0);
st_free_table(w->obj2wmap);
st_free_table(w->wmap2obj);
+ xfree(w);
}
static int
diff --git a/test/ruby/test_weakmap.rb b/test/ruby/test_weakmap.rb
index 4c91661f86..86550cc221 100644
--- a/test/ruby/test_weakmap.rb
+++ b/test/ruby/test_weakmap.rb
@@ -167,4 +167,13 @@ class TestWeakMap < Test::Unit::TestCase
assert_nothing_raised(FrozenError) {@wm[o] = 'foo'}
assert_nothing_raised(FrozenError) {@wm['foo'] = o}
end
+
+ def test_no_memory_leak
+ assert_no_memory_leak([], '', "#{<<~"begin;"}\n#{<<~'end;'}", "[Bug #19398]", rss: true, limit: 1.5, timeout: 60)
+ begin;
+ 1_000_000.times do
+ ObjectSpace::WeakMap.new
+ end
+ end;
+ end
end
diff --git a/version.h b/version.h
index 5f4429bae6..cbdc82a0c9 100644
--- a/version.h
+++ b/version.h
@@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
-#define RUBY_PATCHLEVEL 27
+#define RUBY_PATCHLEVEL 28
#include "ruby/version.h"
#include "ruby/internal/abi.h"