Skip to main content

TOTD #61: How to locally manage/monitor your Rails/Merb applications on JRuby/GlassFish using JMX ?

Posted by arungupta on January 7, 2009 at 9:17 AM PST

style="width: 200px; height: 127px;" alt=""
can easily run both Rails and Merb application. Using href="">JRuby, as opposed to MRI,
allows you to use standard Java monitoring framework and tools. href="">Java
Management Extensions (JMX) is a standard
management and monitoring solution for Java platform. This style="font-weight: bold;">Tip style="font-weight: bold;">Of style="font-weight: bold;">The style="font-weight: bold;">Day
shows how to apply JMX techniques to monitor Rails/Merb applications.

The blog uses a trivial Rails/Merb application to demonstrate
monitoring but the
techniques can be applied to any Rails or Merb application.
FYI  href="">TOTD
#52 shows to create a simple Merb application and href="">TOTD
#28 shows how to create a simple Rails scaffold.

This blog will focus on local monitoring, i.e. monitor a Rails/Merb
running on the same machine as monitoring tool. A subsequent blog will
demonstrate remote monitoring, i.e. monitor a Rails/Merb application
from a different machine.

There is no extra configuration required to enable monitoring if your
Rails/Merb application is running on GlassFish gem. You just need to
launch an additional tool to monitor the already running application.

is a CLI bundled with JDK and launches a graphical tool to monitor and
manage Java applications on a local or remote machine. jconsole can be
used to monitor local application (those running on the same systems as
jconsole) and remote applications (systems other than running

Invoke "jconsole" in a shell as:

style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="2" cellspacing="2">

It shows the following window:


The window shows different Java process that can be managed/monitored
by jconsole. "org.jruby.Main -S glassfish" is the process corresponding
to our Rails/Merb application running using GlassFish Gem. Click on
that process (6179 in this case) and click "Connect".

The href="">jconsole
interface explains the different tabs and meaning of
information displayed on each tab. Here is a snapshot of different tabs
for a trivial Rails application.

href=""> style="border: 0px solid ; width: 700px; height: 547px;" alt=""

It shows heap memory usage, threads, classes loaded, and CPU usage over
a period of time. The "Memory" tab tracks different href="">GC
pools and shows their dynamic growth. Clicking on "Perform
GC" on top-right corner performs a garbage collection (more on this

href=""> style="border: 0px solid ; width: 700px; height: 577px;" alt=""

The "Threads" tab shows the highest number of threads since JVM started
and current number of live daemon and non-daemon threads. Clicking on a
particular thread shows it's current state and stack trace if it's

href=""> style="border: 0px solid ; width: 700px; height: 544px;" alt=""

MBeans tab shows different managed beans registered with the platform
MBean server, i.e. MBean server available in the JDK. As you can see
there are several standard MBeans displayed on the left-hand side from
java.lang and java.util.logging domains.


JRuby runtime also exposes 4 different MBeans - ClassCache, Config,
JITCompiler and ParserStats - all in "org.jruby" domain.
Clicking on
each MBean shows detailed information collected by that MBean as shown

href=""> style="border: 0px solid ; width: 700px; height: 544px;" alt=""

Clicking on "Attributes" node shows the current snapshot of all the
attributes exposed by the MBean as shown below:

href=""> style="border: 0px solid ; width: 700px; height: 539px;" alt=""

Clicking on "Refresh" button will read the fresh values from the MBean.

Some MBeans expose functions as well. Such MBeans have "Operations"
node and clicking on them show the functions exposed as shown below:


These functions can be invoked by clicking on the button in right-hand

These snapshots are captured without even a single invocation of the
application. Accessing "http://localhost:3000/" in a browser updates
heap usage screenshot as shown below:


And permgen is increased because new classes are JITed by JRuby as
shown below:


After a Garbage Collection is performed (by clicking on "Perform GC"
button in "Memory" tab), the updated screenshot looks like:

href=""> style="border: 0px solid ; width: 700px; height: 503px;" alt=""

As the snapshot indicates the "Heap Memory Usage" is back to normal
(which indicates no memory leak) and there is a spike in "CPU Usage"
because CPU cycles are used to perform GC.

Monitoring a Merb application is no different at all. The "Overview"
tab for a trivial Merb app looks like:

href=""> style="border: 0px solid ; width: 700px; height: 546px;" alt=""

The wide range of JMX-compatible
is another advantage of using JRuby over MRI :)

Some other alternatives are using a combination of href="">jps
and jstat.

The href="">JMX
Home Page provides all details about JMX framework. 
Subsequent blogs in this series will discuss:

  • How to remotely manage your Rails/Merb applications using
    JMX API ?
  • How to publish your own metrics that can be managed using
    JMX API ?
  • How to use VisualVM to get more information about the
    underlying VM ?
  • How to use NewRelic/FiveRuns to manage/monitor an
    application ?

How do you monitor your Rails or Merb applications ?

Please leave suggestions on other TOTD (Tip Of The Day) that
you'd like to see.
A complete archive of all tips is available href="">here.

Technorati: totd



Related Topics >>


Cross Language Profiling with JXInsight - JRuby-to-Ruby