From: Charles Oliver Nutter on
Hiya! I just released a gem called jruby_gc_stats, which provides for
JRuby most of the GC-monitoring methods users of Ruby Enterprise
Edition might be using. These methods are provided without any
performance impact using standard JVM monitoring capabilities, and
should work on any mainstream JVM.


Supported methods:

(stats do not incur any perf penalty on JRuby, so these methods just
reset counters)

GC.collections: number of garbage collections since "enable_stats"
GC.time: amount of time in GC since "enable_stats"
GC.growth: uncollected memory increase since last GC run
GC.dump: dump information about all memory pools
GC.allocation_size: increase in total heap size since "enable_stats"
caller_for_all_threads: dump the caller stack for all active threads


GC.num_allocations: no equivalent on JVM
ObjectSpace.live_objects: requires doing a very costly heap dump
ObjectSpace.allocated_objects: requires starting JVM in debug mode


gem install jruby_gc_stats


require 'rubygems'
require 'jruby/gc_stats'

require 'pp'
puts "Enabling stats..."
puts "allocation size: #{GC.allocation_size}"
puts "Running loop..."
1_000.times {
ary = []
1_000.times {ary << 'foo' + 'bar'}
puts "collections: #{GC.collections}"
puts "time: #{GC.time}ms"
puts "bytes since last GC: #{GC.growth}"
puts "size change: #{GC.allocation_size}"
puts "Dumping..."

puts "Dumping caller for all threads..."
2.times { {sleep}}
pp caller_for_all_threads