Last active
October 3, 2015 15:08
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
will define finalizer | |
will run gc | |
did run gc | |
finalize was called |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
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
How about this?