Skip to content

Instantly share code, notes, and snippets.

Last active October 3, 2015 15:08
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(, 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?!?
# I'd like to put the assertion here.
$stderr.puts "did run gc"
Copy link

How about this?

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

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

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

Copy link

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