Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Last active October 3, 2015 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/6caf13104a26210ec525 to your computer and use it in GitHub Desktop.
Save cowboyd/6caf13104a26210ec525 to your computer and use it in GitHub Desktop.
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

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