jmesnil / jmx4r

a JMX library for JRuby

This URL has Read+Write access

jmx4r / examples / memory_on_many_nodes.rb
100755 45 lines (35 sloc) 1.264 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env jruby
require 'rubygems'
require 'jmx4r'
require 'jconsole'
 
# This example will display memory usage for 3 Java applications running locally
# and manageable through diffent JMX ports.
# It will display memory usage, trigger a garbage collection on the 3 Java
# applications and display again the memory usage.
 
ports = [3000, 3001, 3002]
 
# We use jconsole as our target Java applications
# and specify on which port we can connect to their
# MBean server
ports.each { |port| JConsole::start :port => port }
 
# horizontal rule used for display
HR = "+----------------+----------------+----------------+"
 
def display_memory_usages (ports)
  puts HR
  puts "| Node | Heap Used | Non Heap Used |"
  puts HR
 
  ports.each do |port|
    memory = JMX::MBean.find_by_name "java.lang:type=Memory", :port => port
    heap_used = memory.heap_memory_usage["used"]
    non_heap_used = memory.non_heap_memory_usage["used"]
    puts "| localhost:#{port} |#{heap_used.to_s.rjust(15)} |#{non_heap_used.to_s.rjust(15)} |"
  end
 
  puts HR
end
 
puts "Before GC:"
display_memory_usages ports
ports.each do |port|
  memory = JMX::MBean.find_by_name "java.lang:type=Memory", :port => port
  memory.gc
end
puts "After GC:"
display_memory_usages ports