Project

General

Profile

Actions

Bug #10620

closed

#define_singleton_method keeps object from being garbage collected

Added by sean@duke.edu (Sean Dilda) almost 10 years ago. Updated almost 10 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
2.1.5
[ruby-core:66959]

Description

After using #define_singleton_method on an object, the object is never garbage collected

Sample code:
#!/usr/bin/env ruby

obj = Object.new
obj_id = obj.object_id
obj = nil

GC.start
GC.start
GC.start
GC.start
begin
ObjectSpace._id2ref obj_id
puts "GC failed, object still exists"
rescue RangeError
puts "GC worked"
end

obj = Object.new
obj.define_singleton_method(:f) { "F" }
obj_id = obj.object_id
obj = nil

GC.start
GC.start
GC.start
GC.start
begin
ObjectSpace._id2ref obj_id
puts "GC failed, object still exists"
rescue RangeError
puts "GC worked"
end

This outputted:
GC worked
GC failed, object still exists

The object without the singleton method was cleaned up properly, the object with defined_singleton_method used never got cleaned up.

Updated by normalperson (Eric Wong) almost 10 years ago

Try the following infinite loop, I see stable memory usage:

loop do
  obj = Object.new
  obj.define_singleton_method(:f) { "F" }
end

Not even multiple calls to GC.start can guarantee an object is
collected, as the current Ruby GC is conservative and not precise.
Precise GC should solve the problem, but that is a lot of work
(and we'd still need a conservative part for 3rd party extensions
for years)

Updated by sean@duke.edu (Sean Dilda) almost 10 years ago

Thanks for the quick response.

I tried your code and saw what you did.. it cleans up the objects, just a lot slower than I expected.
Please feel free to close out this bug. I'd do so, but I'm not seeing the option.

Updated by shugo (Shugo Maeda) almost 10 years ago

  • Status changed from Open to Closed

Updated by usa (Usaku NAKAMURA) almost 10 years ago

  • Status changed from Closed to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0