<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://brokenco.de//feed/by_tag/visualvm.xml" rel="self" type="application/atom+xml" /><link href="https://brokenco.de//" rel="alternate" type="text/html" /><updated>2026-10-31T17:58:32+00:00</updated><id>https://brokenco.de//feed/by_tag/visualvm.xml</id><title type="html">rtyler</title><subtitle>a moderately technical blog</subtitle><author><name>R. Tyler Croy</name></author><entry><title type="html">Profiling of remote JVMs with VisualVM and JConsole</title><link href="https://brokenco.de//2014/07/07/profiling-remote-jvms.html" rel="alternate" type="text/html" title="Profiling of remote JVMs with VisualVM and JConsole" /><published>2014-07-07T00:00:00+00:00</published><updated>2014-07-07T00:00:00+00:00</updated><id>https://brokenco.de//2014/07/07/profiling-remote-jvms</id><content type="html" xml:base="https://brokenco.de//2014/07/07/profiling-remote-jvms.html"><![CDATA[<p><strong>Note:</strong> I originally posted this
<a href="http://hackers.lookout.com/2014/06/profiling-remote-jvms/">here</a>, on the
<a href="http://hackers.lookout.com">Lookout hackers blog</a>. I encourage you to check
the blog out and follow <a href="https://twitter.com/lookouteng">@LookoutEng</a>.</p>

<hr />

<p>Recently I found myself hosting a bit of a “bake-off competition” between
servlet containers for JRuby applications here at
<a href="https://www.lookout.com/about/careers">Lookout</a>.</p>

<p>The goal of the bake-off was to determine whether we should host some
<a href="https://github.com/jruby/warbler">warbled</a> JRuby applications in Tomcat or
Jetty. Not having a huge amount of experience, or bias, towards one or the
other I elected to run a simple <a href="https://github.com/rtyler/hellowarld">Hello
Warld</a> application in both and see how
well the containers performed.</p>

<h3 id="the-bake-off-environment">The Bake-Off Environment</h3>

<p>For the bake-off of servlet containers I used to identical EC2 instances. EC2
instances were chosen instead of running both containers on my local machine to
keep the machines consistent, isolated and more representative of the
environment we would be running webapps in. The test bed specs were:</p>

<ul>
  <li>Ubuntu 12.04 LTS</li>
  <li><code class="language-plaintext highlighter-rouge">m1.small</code> instance size (hey, I’m not made of money!)</li>
  <li>us-west-2 region</li>
  <li>A security group with everything open to the other machines in that security
group. This is important for later.</li>
  <li>OpenJDK 7 (u55)</li>
</ul>

<p>The machines were then provisioned with the Tomcat 7 and Jetty 6 respectively,
only because those were available directly from the native packages on Ubuntu 12.04.</p>

<p>Both containers were also set up to perform hot-deploys; a feature which relies
on live-reloading of an application without restarting the JVM.</p>

<h3 id="problems-ahoy">Problems, ahoy!</h3>

<p>After performing a number of successive hot-deploys in Tomcat, I found my logs
clobbered with the following errors:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space
Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space
Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space
</code></pre></div></div>

<p>The actual problem here will need to be covered in another blog post, but
something fishy was clearly going on with hot-deployments in Tomcat.</p>

<h2 id="profiling-in-the-cloud">Profiling in the cloud</h2>

<p>My favorite tool for understanding a running JVM is
definitely <a href="http://visualvm.java.net/">VisualVM</a>, with
<a href="http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html">JConsole</a>
running in a close second place. Fortunately, both tools are quite easy to set
up for connecting to remote JVMs.</p>

<h3 id="setting-up-jstatd">Setting up jstatd</h3>

<p>Previously I mentioned the importance of the EC2 security group. It’s important
that port <strong>1099</strong> is open within the security group. This is the port
<a href="http://docs.oracle.com/javase/7/docs/technotes/tools/share/jstatd.html">jstatd</a>
runs on by default. <code class="language-plaintext highlighter-rouge">jstatd</code> is what will provide VisualVM with live
instrumentation data from the JVMs running on the machines.</p>

<p>It’s also important to provide a liberal security policy file to <code class="language-plaintext highlighter-rouge">jstatd</code>. If
this were anything more than a simple test implementation, I would recommend a
more restrictive policy, but we’re optimizing for easiness here, so a wide open
policy is fine:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>grant codebase "file:${java.home}/../lib/tools.jar" {
    permission java.security.AllPermission;
};
</code></pre></div></div>

<p>Save the policy above into a file named <code class="language-plaintext highlighter-rouge">jstatd.policy</code>, which we can run
on each machine:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ec2-tomcat% sudo jstatd -J-Djava.security.policy=./jstatd.policy &amp;
</code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ec2-jetty% sudo jstatd -J-Djava.security.policy=./jstatd.policy &amp;
</code></pre></div></div>

<h3 id="jmx-for-interactivity">JMX for interactivity</h3>

<p><code class="language-plaintext highlighter-rouge">jstatd</code> only gives us half the picture we want. We also want
<a href="http://www.oracle.com/technetwork/java/javase/tech/javamanagement-140525.html">JMX</a>
to be configured for both containers to allow us to extract more information
and interact with the running JVMs.</p>

<p>Again, we’ll set up very liberal security policies since this is for testing
only! The native packages for both containers put a file in <code class="language-plaintext highlighter-rouge">/etc/defaults</code>
which contains a <code class="language-plaintext highlighter-rouge">JAVA_OPTS</code> variable, to which the following should be added:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.port=1098
</code></pre></div></div>

<p>After restarting the container processes, they will now have the right JMX
settings and we should be able to finally be able to connect VisualVM or
JConsole to the JVMs.</p>

<h3 id="proxying-for-connectivity">Proxying for connectivity</h3>

<p>In order to give tools running locally on my machine access to these processes
running inside of a security group within EC2, I’ll rely on <code class="language-plaintext highlighter-rouge">ssh</code>’s ability to
provide a SOCKS5 proxy;</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>kiwi% ssh -D 9696 ubuntu@ec2-66-166-66-66.us-west-2.compute.amazonaws.com
</code></pre></div></div>

<p>This will provide a path for both VisualVM and JConsole to use when accessing
the JMX information (port 1098) and the <code class="language-plaintext highlighter-rouge">jstatd</code> information (port 1099) on the
machines running inside of a security group within EC2. While not wholly
necessary, exposing these ports to the wide-open internet seems like a Bad
Idea™.</p>

<h4 id="running-jconsole-with-a-proxy">Running JConsole with a proxy</h4>

<p>JConsole doesn’t have any GUI configuration for a proxy, so it’s necessary to
set some command line parameters:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>kiwi% jconsole -J-DsocksProxyHost=localhost -J-DsocksProxyPort=9696
</code></pre></div></div>

<p>Once JConsole is up and running, you only need to enter the EC2 hostname and
appropriate JMX port (1098) to connect to the running JVM:</p>

<p><img src="http://hackers.lookout.com/images/post-images/profiling-remote-jvms/jconsole-with-proxy.png" alt="JConsole with EC2
hostnames" /></p>

<p>After clicking “Connect”, JConsole will use the SSH-based proxy to connect to
the host, and you should be able to poke around with a real live JVM:</p>

<p><img src="http://hackers.lookout.com/images/post-images/profiling-remote-jvms/jconsole-connected-remotely.png" alt="JConsole connected to
EC2" /></p>

<h3 id="running-visualvm-with-a-proxy">Running VisualVM with a proxy</h3>

<p>Unlike JConsole, VisualVM allows for a GUI-based configuration of a SOCKS
proxy:</p>

<p><img src="http://hackers.lookout.com/images/post-images/profiling-remote-jvms/visualvm-proxy-conf.png" alt="VisualVM proxy
configuration" /></p>

<p>With the proxy configuration saved, we can then add a remote host by
right-clicking on “Remote” and selecting “Add remote host”.</p>

<p><img src="http://hackers.lookout.com/images/post-images/profiling-remote-jvms/adding-remote-host-visualvm.png" alt="Adding remote VisualVM
host" /></p>

<p>Provided <code class="language-plaintext highlighter-rouge">jstatd</code> is running on the remote host, your SSH-based proxy is
running and the remote JVM is running, you should be able to connect to the
remote JVM and start profiling it like you would a local JVM!</p>

<p><img src="http://hackers.lookout.com/images/post-images/profiling-remote-jvms/profiling-tomcat-remotely.png" alt="Profiling Tomcat
remotely" /></p>

<hr />

<p>Both JConsole and VisualVM give you access to a lot of the instrumentation data
available from a running Java Virtual Machine, but neither will magically
identify or solve performance problems. There’s still more work to be done to
triage and ultimately resolve those kinds of issues, but at least these tools
give you the information you need to know what’s going on, and <a href="http://cdn.churchm.ag/wp-content/uploads/2014/03/knowing-is-half-the-battle.jpg">knowing is half
the
battle</a>.</p>]]></content><author><name>R. Tyler Croy</name></author><category term="jvm" /><category term="jruby" /><category term="visualvm" /><summary type="html"><![CDATA[Note: I originally posted this here, on the Lookout hackers blog. I encourage you to check the blog out and follow @LookoutEng.]]></summary></entry></feed>