Last active
August 4, 2016 06:04
-
-
Save mike-bourgeous/a52d88f91e28cb8bd7e99de2068d0b7a to your computer and use it in GitHub Desktop.
Convenient way to get GC stats for a block of Ruby code.
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
def gc_stat | |
GC.disable | |
asym = :total_allocated_objects | |
fsym = :total_freed_objects | |
before = GC.stat | |
ba = before[asym] | |
bf = before[fsym] | |
puts "Before: alloc=#{ba} free=#{bf}" | |
start = Time.now | |
ret = yield | |
elapsed = Time.now - start | |
after = GC.stat | |
aa = after[asym] | |
af = after[fsym] | |
puts "After: alloc=#{aa} free=#{af}" | |
puts "Delta: alloc=#{aa - ba} free=#{af - bf} elapsed=#{elapsed}" | |
GC.enable | |
ret | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment