Skip to content

Instantly share code, notes, and snippets.

@mike-bourgeous
Last active August 4, 2016 06:04
Show Gist options
  • Save mike-bourgeous/a52d88f91e28cb8bd7e99de2068d0b7a to your computer and use it in GitHub Desktop.
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.
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