[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
```
This commit is contained in:
Peter Zhu 2023-02-01 09:08:57 -05:00
parent 375f527ded
commit c6f84e9189
Notes: git 2023-02-01 18:24:16 +00:00
2 changed files with 10 additions and 0 deletions

1
gc.c
View File

@ -12870,6 +12870,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

View File

@ -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