diff options
| author | NARUSE, Yui <nurse@users.noreply.github.com> | 2024-03-14 23:18:15 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-14 14:18:15 +0000 |
| commit | fafe5db7328eb3395ac4559992701b5f25ab49f4 (patch) | |
| tree | 2ba88c047f228889a6e74940f79f0b1db9b4f622 /test/ruby/test_string.rb | |
| parent | 2a84aaf4a8c8d6d6bbb09416711922532b0033fe (diff) | |
merge revision(s) b3d612804946e841e47d14e09b6839224a79c1a4: [Backport #20150] (#10253)
Fix memory leak in grapheme clusters
[Bug #20150]
String#grapheme_cluters and String#each_grapheme_cluster leaks memory
because if the string is not UTF-8, then the created regex will not
be freed.
For example:
str = "hello world".encode(Encoding::UTF_32LE)
10.times do
1_000.times do
str.grapheme_clusters
end
puts `ps -o rss= -p #{$$}`
end
Before:
26000
42256
59008
75792
92528
109232
125936
142672
159392
176160
After:
9264
9504
9808
10000
10128
10224
10352
10544
10704
10896
---
string.c | 98 +++++++++++++++++++++++++++++++-----------------
test/ruby/test_string.rb | 11 ++++++
2 files changed, 75 insertions(+), 34 deletions(-)
Diffstat (limited to 'test/ruby/test_string.rb')
| -rw-r--r-- | test/ruby/test_string.rb | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 333b610bc5..4aa119f8fd 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1108,6 +1108,17 @@ CODE assert_equal("C", res[2]) end + def test_grapheme_clusters_memory_leak + assert_no_memory_leak([], "", "#{<<~"begin;"}\n#{<<~'end;'}", "[Bug #todo]", rss: true) + begin; + str = "hello world".encode(Encoding::UTF_32LE) + + 10_000.times do + str.grapheme_clusters + end + end; + end + def test_each_line verbose, $VERBOSE = $VERBOSE, nil |
