Skip to content

Instantly share code, notes, and snippets.

@headius
Created December 29, 2011 12:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/1533906 to your computer and use it in GitHub Desktop.
Save headius/1533906 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment