Monitoring per-thread allocation using JRuby and java.lang.management.ThreadMXBean
| require 'java' | |
| require 'jruby' | |
| thread_bean = java.lang.management.ManagementFactory.thread_mx_bean | |
| t1 = Thread.new do | |
| a = nil | |
| loop do | |
| a = 'foo' | |
| end | |
| end | |
| t1_thread = JRuby.reference(t1).native_thread | |
| main = Thread.current | |
| main_thread = JRuby.reference(main).native_thread | |
| loop do | |
| sleep 1 | |
| t1_alloc = thread_bean.get_thread_allocated_bytes([t1_thread.id].to_java(:long))[0] | |
| main_alloc = thread_bean.get_thread_allocated_bytes([main_thread.id].to_java(:long))[0] | |
| puts "main allocated: #{main_alloc}" | |
| puts "t1 allocated: #{t1_alloc}" | |
| end |