Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Last active Oct 3, 2015
Embed
What would you like to do?
How can you actually force garbage collection to run fully for unit testing?
will define finalizer
will run gc
did run gc
finalize was called
# I'm trying write some unit tests around some finalization logic.
# What I would like is some way to make sure that a given object was garbage
# collected. The only surefire way that I know to do this is to assert
# that a finalizer was called.
#
# The problem is knowing when all finalizers that can be run, have been run, so
# how to accomplish this?
$stderr.puts "will define finalizer"
ObjectSpace.define_finalizer(Object.new, proc { $stderr.puts "finalize was called" })
$stderr.puts "will run gc"
GC.start full_mark: true, immediate_sweep: true
# I think the GC has collected, but the finalizers are queued somewhere to run
# How can we make them run?!?
Thread.pass
# I'd like to put the assertion here.
$stderr.puts "did run gc"
@jeremywrowe
Copy link

jeremywrowe commented Oct 2, 2015

How about this?

class TheFinalOne
  def initialize(thing)
    ObjectSpace.define_finalizer(self, self.class.finalize(thing))
  end

  def self.finalize(thing)
    proc { puts thing }
  end
end

# spec
describe TheFinalOne do
  it "a winner is you" do
    expect(TheFinalOne).to receive(:finalize).with("some thing")
    TheFinalOne.new("some thing")
  end
end

@cowboyd
Copy link
Author

cowboyd commented Oct 3, 2015

Sadly, I end up with the same result. I'm beginning to think that it will require a C extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment