Thread.new do GC::Profiler.enable current_message = "" epoch = Time.now total_time = 0 gc_events = 0 heap_use = 0 heap_size = 0 fmt = "%7s %5s %15s %15s %10s" Thread.new do loop do # TODO: Could drop GC events added between accessing them and clearing. total_time += GC::Profiler.total_time GC::Profiler.raw_data.each do |gc_event| heap_use = gc_event[:HEAP_USE_SIZE] heap_size = gc_event[:HEAP_TOTAL_SIZE] gc_events += 1 end GC::Profiler.clear current_message = fmt % [ "%.2f" % (Time.now - epoch), gc_events, "%.2f" % (heap_use / 1024.0 / 1024), "%.2f" % (heap_size / 1024.0 / 1024), "%.3f" % total_time ] sleep 0.5 end end server = TCPServer.new 8787 loop do Thread.start(server.accept) do |client| begin client.puts fmt % [ "Time", "#", "Heap Use (mb)", "Heap Size (mb)", "GC Time (s)" ] loop do client.puts current_message sleep 0.5 end rescue Errno::EPIPE end end end end