diff options
author | Peter Zhu <[email protected]> | 2025-03-11 15:05:05 -0400 |
---|---|---|
committer | Peter Zhu <[email protected]> | 2025-03-11 21:55:03 -0400 |
commit | 1cdec3240b3c998c0cbf73556786aa3fa0b02ae7 (patch) | |
tree | 2fced782c8831c25f95be7e3253fde22253886cf /test/ruby/test_regexp.rb | |
parent | 1b2cc9c2b80da4618499ab3dd3dd70a499546c75 (diff) |
Fix memory leak in rb_reg_search_set_match
https://github.com/ruby/ruby/pull/12801 changed regexp matches to reuse
the backref, which causes memory to leak if the original registers of the
match is not freed.
For example, the following script leaks memory:
10.times do
1_000_000.times do
"aaaaaaaaaaa".gsub(/a/, "")
end
puts `ps -o rss= -p #{$$}`
end
Before:
774256
1535152
2297360
3059280
3821296
4583552
5160304
5091456
5114256
4980192
After:
12480
11440
11696
11632
11632
11760
11824
11824
11824
11888
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/12905
Diffstat (limited to 'test/ruby/test_regexp.rb')
-rw-r--r-- | test/ruby/test_regexp.rb | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index f22128686e..be7c6761ca 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -999,6 +999,18 @@ class TestRegexp < Test::Unit::TestCase assert_equal('foobazquux/foobazquux', result, bug8856) end + def test_regsub_no_memory_leak + assert_no_memory_leak([], "#{<<~"begin;"}", "#{<<~"end;"}", rss: true) + code = proc do + "aaaaaaaaaaa".gsub(/a/, "") + end + + 1_000.times(&code) + begin; + 100_000.times(&code) + end; + end + def test_ignorecase v = assert_deprecated_warning(/variable \$= is no longer effective/) { $= } assert_equal(false, v) |