From: nobu@... Date: 2014-11-25T12:02:41+00:00 Subject: [ruby-core:66457] [ruby-trunk - Bug #10537] Repeated creation and garbage collection of WeakRef instances against a single object leaks memory Issue #10537 has been updated by Nobuyoshi Nakada. Description updated I can't reproduce the error, too. What about this patch? ~~~diff diff --git a/gc.c b/gc.c index 9c0dbef..f4c4e93 100644 --- a/gc.c +++ b/gc.c @@ -7869,10 +7869,10 @@ wmap_aset(VALUE self, VALUE wmap, VALUE orig) TypedData_Get_Struct(self, struct weakmap, &weakmap_type, w); should_be_finalizable(orig); should_be_finalizable(wmap); - define_final0(orig, w->final); - define_final0(wmap, w->final); - st_update(w->obj2wmap, (st_data_t)orig, wmap_aset_update, wmap); - st_insert(w->wmap2obj, (st_data_t)wmap, (st_data_t)orig); + if (!st_update(w->obj2wmap, (st_data_t)orig, wmap_aset_update, wmap)) + define_final0(orig, w->final); + if (!st_insert(w->wmap2obj, (st_data_t)wmap, (st_data_t)orig)) + define_final0(wmap, w->final); return nonspecial_obj_id(orig); } ---------------------------------------- Bug #10537: Repeated creation and garbage collection of WeakRef instances against a single object leaks memory https://bugs.ruby-lang.org/issues/10537#change-50080 * Author: Alex Boyd * Status: Open * Priority: Normal * Assignee: * Category: lib * Target version: * ruby -v: ruby 2.2.0dev (2014-11-24 trunk 48552) [x86_64-darwin14] * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN ---------------------------------------- ~~~ruby require 'weakref' a = Object.new 1_000_000.times do WeakRef.new a end GC.start ~~~ The above results in Ruby consuming ~150 MB of RAM, all of which can only be freed by dropping `a`. This should not be the case - an object being weakly referenced should not itself hold a reference to the WeakRef (or any associated data) pointing at it. -- https://bugs.ruby-lang.org/