<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jeff Sinclair's Website &#187; jvm</title>
	<atom:link href="http://www.cooljeff.co.uk/tag/jvm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cooljeff.co.uk</link>
	<description>my 2 pence to the world</description>
	<lastBuildDate>Sat, 12 Dec 2009 22:17:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Garbage collection of the permanent generation (permgen)</title>
		<link>http://www.cooljeff.co.uk/2009/11/01/garbage-collection-of-the-permanent-generation-permgen/</link>
		<comments>http://www.cooljeff.co.uk/2009/11/01/garbage-collection-of-the-permanent-generation-permgen/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 09:45:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[hotspot]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://www.cooljeff.co.uk/?p=153</guid>
		<description><![CDATA[There is often confusion around what the permanent generation contains and given its name, whether or not it can be garbage collected. To cut a long story short, the permanent generation can be garbage collected and is where reflective class meta data, as well as string constants are stored. 
This blog entry takes a practical [...]]]></description>
			<content:encoded><![CDATA[<p>There is often confusion around what the permanent generation contains and given its name, whether or not it can be garbage collected. To cut a long story short, the permanent generation can be garbage collected and is where reflective class meta data, as well as string constants are stored. </p>
<p>This blog entry takes a practical approach to answer the following questions:</p>
<ul>
<li>Can the permanent generation be garbage collected?</li>
<li>Does the value of -Xmx (the maximum heap size) include the permanent generation?</li>
</ul>
<h4>Can the permanent generation be garbage collected?</h4>
<p>Yes it can. There is often the misconception that string constants cannot be garbage collected. However the only requirement that the JVM specification stipulates is that identity comparison works for constant string values. There is no requirement that the constant be the same object throughout the lifetime of the JVM. Hence if there are no references left to a string constant, there is no reason why it cannot be garbage collected.</p>
<p>To prove this take the following code:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PermGenDemo <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-weight: bold;">int</span> i = <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000;  font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;string-&quot;</span> + ++i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">intern</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>If the above code is run using the following command:</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
$ java -verbose:<span style="color: #c20cb9; font-weight: bold;">gc</span> -XX:<span style="color: #007800;">PermSize</span>=8m -XX:<span style="color: #007800;">MaxPermSize</span>=64m PermGenDemo</pre>
<p>The following output will be seen (note output cut for brevity):</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>Full GC <span style="color: #7a0874; font-weight: bold;">&#91;</span>PSYoungGen: 32K-<span style="color: #000000; font-weight: bold;">&gt;</span>0K<span style="color: #7a0874; font-weight: bold;">&#40;</span>28928K<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> . . . <span style="color: #7a0874; font-weight: bold;">&#91;</span>PSPermGen: 65535K-<span style="color: #000000; font-weight: bold;">&gt;</span>6461K<span style="color: #7a0874; font-weight: bold;">&#40;</span>65536K<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>Full GC <span style="color: #7a0874; font-weight: bold;">&#91;</span>PSYoungGen: 32K-<span style="color: #000000; font-weight: bold;">&gt;</span>0K<span style="color: #7a0874; font-weight: bold;">&#40;</span>31168K<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> . . . <span style="color: #7a0874; font-weight: bold;">&#91;</span>PSPermGen: 65535K-<span style="color: #000000; font-weight: bold;">&gt;</span>6461K<span style="color: #7a0874; font-weight: bold;">&#40;</span>65536K<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre>
<p>The above conclusively shows that the permanent generation is collected and when it requires collecting, triggers a full collection.</p>
<h4>Does the value of -Xmx (the maximum heap size) include the permanent generation?</h4>
<p>No it does not. The size of the permanent generation is controlled by the following JVM options:</p>
<p>   * <em>-XX:MaxPermSize</em> controls the maximum permanent generation size.<br />
   * <em>-XX:PermSize</em> controls the initial permanent generation size.</p>
<p>The permanent generation is like any other generational compartment: it can grow to a maximum, technically can shrink and can also be garbage collected.</p>
<p>To demonstrate that the permanent generation is not included in the maximum heap settings, pmap can be used (e.g. by running <em>pmap &lt;pid&gt; | tail -1</em>).</p>
<p>The following table illustrates the output of pmap using different JVM heap and permanent generation settings:</p>
<table border="1">
<tr>
<td><strong>JVM options</strong></td>
<td><strong>pmap output</strong></td>
</tr>
<tr>
<td>java -Xms256m -Xmx256m -XX:PermSize=64m -XX:MaxPermSize=64m</td>
<td>512204K</td>
</tr>
<tr>
<td>java -Xms512m -Xmx512m -XX:PermSize=64m -XX:MaxPermSize=64m</td>
<td>760556K</td>
</tr>
<tr>
<td>java -Xms512m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=128m</td>
<td>825188K</td>
</tr>
</table>
<p>In the above <em>-Xms=-Xmx</em> and <em>-XX:PermSize=-XX:MaxPermSize</em> are set deliberately to stop the heap expanding and ensure constant results. The first value of <em>512204K</em> gives us a base value which takes into account any overhead introduced by native libraries (including the jvm itself) being mapped into memory. The second value of <em>760556K</em> is an exact increase of <em>256m</em>, which is exactly how much the heap was increased by. The third value of <em>825188K</em> is an exact increase of <em>64m</em>, which is exactly how much the permanent generation was increased by. </p>
<p>The above results clearly show that the settings for permgen are unrelated to that of the heap.</p>
<h4>Conclusion</h4>
<ul>
<li>The permanent generation can be garbage collected like other generations.</li>
<li>The permanent generation is additional memory to the java heap.</li>
<li><em>-XX:MaxPermSize</em> controls the maximum memory used by the permanent generation.</li>
<li><em>-XX:PermSize</em> controls the initial memory used by the permanent generation.</li>
</ul>
<h4>Resources</h4>
<ol>
<li><a href=" http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp">Java Hotspot VM Options</a></li>
<li><a href="http://blogs.sun.com/jonthecollector/entry/presenting_the_permanent_generation">Presenting the Permanent Generation</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.cooljeff.co.uk/2009/11/01/garbage-collection-of-the-permanent-generation-permgen/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The subtleties of overriding package private methods</title>
		<link>http://www.cooljeff.co.uk/2009/05/03/the-subtleties-of-overriding-package-private-methods/</link>
		<comments>http://www.cooljeff.co.uk/2009/05/03/the-subtleties-of-overriding-package-private-methods/#comments</comments>
		<pubDate>Sun, 03 May 2009 15:41:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://www.cooljeff.co.uk/?p=120</guid>
		<description><![CDATA[The Java Language comes with several modifiers for controlling access to methods: public, private, protected and package private. 
In general we assume the following rules govern these access modifiers:

A public method can be accessed by any other method.
A private method can only be accessed by the class that it is declared in.
A protected method can [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6">Java Language</a> comes with several modifiers for controlling access to methods: public, private, protected and package private. </p>
<p>In general we assume the following rules govern these access modifiers:</p>
<ul>
<li>A public method can be accessed by any other method.</li>
<li>A private method can only be accessed by the class that it is declared in.</li>
<li>A protected method can be accessed by any class in the same package or any subclass.</li>
<li>A package private method can be accessed by any class in the same package.</li>
</ul>
<p>The above would be accepted as suitable answers at most interviews, however there are some subtleties seen in the behaviour of package private that cannot be explained using this simple definition alone.</p>
<p>Take the following piece of code that represents a <em>Square</em>:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">uk.co.cooljeff.access</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Square <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #006600; font-weight: bold;">float</span> length<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> Square<span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">float</span> length<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">length</span> = length<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">float</span> calculateArea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> length <span style="color: #339933;">*</span> length<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a> getColour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Red&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Take the following piece of code that represents a <em>CustomSquare</em>:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">uk.co.cooljeff.access</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">uk.co.cooljeff.access.Square</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomSquare <span style="color: #000000; font-weight: bold;">extends</span> Square <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> CustomSquare<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  @<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Override.html"><span style="color: #003399; font-weight: bold;">Override</span></a>
  <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a> getColour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Blue&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>From the simple definition of package private above, we would expect the following piece of code to print out <em>Blue</em> as the colour of <em>CustomSquare</em>:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Printer <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> print<span style="color: #009900;">&#40;</span>Square square<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #003399; font-weight: bold;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Square of type &quot;</span> + square.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                                   + <span style="color: #0000ff;">&quot; has colour &quot;</span> + square.<span style="color: #006633;">getColour</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Printer printer = <span style="color: #000000; font-weight: bold;">new</span> Printer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    printer.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> CustomSquare<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>For the majority of people who run the above code, indeed <em>Blue</em> is printed. However for one of my colleagues at work who tried to do something similar, <em>Red</em> was always observed.</p>
<p>To try to explain why <em>Red</em> is sometimes seen, we need to understand what the JVM is trying to do. If you decompile the <em>Printer</em> class you will see that the <em>invokevirutal</em> instruction is used to invoke the <em>Square.getColour()</em> method:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> print<span style="color: #009900;">&#40;</span>uk.<span style="color: #006633;">co</span>.<span style="color: #006633;">cooljeff</span>.<span style="color: #006633;">access</span>.<span style="color: #006633;">Square</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  Code:
   <span style="color: #cc66cc;">25</span>:	invokevirtual	#<span style="color: #cc66cc;">59</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Method uk/co/cooljeff/access/Square.getColour:()Ljava/lang/String;</span>
   <span style="color: #cc66cc;">37</span>:	<span style="color: #000000; font-weight: bold;">return</span></pre>
<p>For virtual methods, there is a lookup algorithm which is used to locate the exact method to invoke. Specifically the following is given for the <em>invokevirtual</em> instruction (paraphrasing):</p>
<blockquote><p>
Let C be the class of the target of the method invocation. The actual method to be invoked is selected by the following lookup procedure:</p>
<ul>
<li>If C contains a declaration for an instance method M with the same name and descriptor as the resolved method, and the resolved method is accessible from C, then M is the method to be invoked, and the lookup procedure terminates.</li>
</ul>
</blockquote>
<p>Hence using this definition, I would expect that <em>CustomSquare.getColour()</em> would be considered since it matches the same name and descriptor as <em>Square.getColour()</em>. The question then becomes is this method accessible from <em>Printer</em>. To answer this we need to look at the <a href="http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html#75929">Access Control (5.4.4)</a> definition in the <a href="http://java.sun.com/docs/books/jvms/"> JVM Specification</a> which states (paraphrasing):</p>
<blockquote><p>A field or method R is accessible to a class or interface D if and only if any of the following conditions are true:</p>
<ul>
<li>&#8230;</li>
<li>R is package private and is declared by a class in the same runtime package as D.</li>
</ul>
</blockquote>
<p>There is a subtlety here in that the phrase <strong>runtime package</strong> is used. At runtime a class is uniquely defined by its fully qualified name and its ClassLoader. The runtime package takes into account not only the compile time package but also the ClassLoader that loaded the class.</p>
<p>Taking the fact that the ClassLoader could have something to do with why <em>Red</em> is sometimes observed, lets redefine our <em>Printer</em> to load the <em>CustomSquare</em> in a different ClassLoader:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">uk.co.cooljeff.access</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLClassLoader</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Printer <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> print<span style="color: #009900;">&#40;</span>Square square<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #003399; font-weight: bold;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Square of type &quot;</span> + square.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> + <span style="color: #0000ff;">&quot; has colour &quot;</span> + square.<span style="color: #006633;">getColour</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Exception.html"><span style="color: #003399; font-weight: bold;">Exception</span></a> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Path to a jar or classes containing CustomSquare.</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a> customSquareURL = <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #003399; font-weight: bold;">System</span></a>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;customsquare.classpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html"><span style="color: #003399; font-weight: bold;">URL</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> urls = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html"><span style="color: #003399; font-weight: bold;">URL</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html"><span style="color: #003399; font-weight: bold;">URL</span></a><span style="color: #009900;">&#40;</span> customSquareURL <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLClassLoader.html"><span style="color: #003399; font-weight: bold;">URLClassLoader</span></a> loader = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLClassLoader.html"><span style="color: #003399; font-weight: bold;">URLClassLoader</span></a><span style="color: #009900;">&#40;</span>urls<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html"><span style="color: #003399; font-weight: bold;">Class</span></a> clazz = loader.<span style="color: #006633;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;uk.co.cooljeff.access.CustomSquare&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Printer printer = <span style="color: #000000; font-weight: bold;">new</span> Printer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    printer.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Square<span style="color: #009900;">&#41;</span> clazz.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>When <em>CustomSquare</em> is loaded by a different ClassLoader, the output is <em>Red</em>, which is what my colleague observed.</p>
<p>At this stage, although the behaviour is reproducible, it cannot be explained using the 2nd Edition of the JVM Specification alone. The specification clearly states in the definition of method resolution, that an <em>IllegalAccessError</em> should be thrown if the method is not accessible and the definition of accessibility clearly states that <em>CustomSquare</em> is not accessible. So why do we not get an <em>IllegalAccessError</em> in either Sun&#8217;s HotSpot JVM or IBM&#8217;s JVM?</p>
<p>The behaviour made sense to me because it provides a way to prevent hacks such as the one my colleague was attempting to do, however I could not prove it using the JVM Specification. It was time to call on the experts so I started a mailthread titled <a href="http://mail.openjdk.java.net/pipermail/hotspot-dev/2009-April/001528.html">invokevirtual on package private override in different classloader</a> on the <a href="http://mail.openjdk.java.net/pipermail/hotspot-dev/">hotspot-dev mailing list</a>.</p>
<p>As it turns out there was an <a href="[http://java.sun.com/docs/books/jvms/second_edition/jvms-clarify.html">amendment</a> made to the 2nd Edition of the JVM Specification relating to this behaviour which revises the definition of <em>invokevirtual</em>. Specifically it replaces the accessibility constraint in favour for an override constraint. Hence to explain the behaviour we don&#8217;t look at whether <em>CustomSquare.getColour()</em> is accessible, we need to determine if it overrides <em>Square.getColour()</em>.</p>
<p>The answer to the override question is no, which explains why the behaviour is observed. However as pointed out on the HotSpot mail thread, I was not happy that the amendment to the JVM specification refered to a language definition (namely override). At the moment in order to explain the behaviour we need to use the revised definition of <em>override</em> in the Java Language Specification. This goes against the fundamental principle that the Java Virtual Machine Specification is decoupled from the Java Language Specification. This was acknowledged on the mailthread: Alex Buckley replied informing me that the 3rd edition of the JVM specification is independent of the Language specification. Specifically the 3rd edition (not publicly available yet) drops the chapter on the Java Programming Language Concepts entirely and gains a JVM specific definition of <em>overrides</em>. </p>
<p>So I&#8217;m happy now, I can explain the behaviour <img src='http://www.cooljeff.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I&#8217;d like to thank the following people who helped me understand the behaviour discussed in this blog entry: Karen Kinnear (Sun Microsystems), David Holmes (Sun Microsystems) and Alex Buckley (Sun Microsystems).</p>
<p>Resources:</p>
<ul>
<li><a href="http://java.sun.com/docs/books/jls/">The Java Language Specification</a></li>
<li><a href="http://java.sun.com/docs/books/jvms/">The Java Virtual Machine Specification</a></li>
<li><a href="http://java.sun.com/docs/books/jvms/second_edition/jvms-clarify.html">Clarifications and Amendments to the Java Virtual Machine Specification</a></li>
<li><a href="http://mail.openjdk.java.net/pipermail/hotspot-dev/2009-April/001528.html">invokevirtual discussion on hotspot-dev mail list</a></li>
<li><a href="http://java.sun.com/docs/books/jls/method-override-rationale.html">Why the Definition of Method Override was Revised</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.cooljeff.co.uk/2009/05/03/the-subtleties-of-overriding-package-private-methods/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Detecting all running JVMs</title>
		<link>http://www.cooljeff.co.uk/2009/04/05/detecting-all-running-jvms/</link>
		<comments>http://www.cooljeff.co.uk/2009/04/05/detecting-all-running-jvms/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 21:19:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[hotspot]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://www.cooljeff.co.uk/?p=108</guid>
		<description><![CDATA[Several well known JVM tools appear to be able to magically detect the Java processes running on a system. Good examples of this are: jps and Visual VM. As of Java 6 it is even possible to find out yourself which Java processes are running on the local machine that you have access to using [...]]]></description>
			<content:encoded><![CDATA[<p>Several well known JVM tools appear to be able to magically detect the Java processes running on a system. Good examples of this are: <a href="http://java.sun.com/javase/6/docs/technotes/tools/share/jps.html" target="_blank">jps</a> and <a href="https://visualvm.dev.java.net/" target="_blank">Visual VM</a>. As of Java 6 it is even possible to find out yourself which Java processes are running on the local machine that you have access to using the <a href="http://java.sun.com/javase/6/docs/technotes/guides/attach/index.html" target="_blank">Dynamic Attach API</a>. These APIs can be found in the tools.jar (JAVA_HOME/lib/tools.jar).</p>
<p>The following code illustrates how the Dynamic Attach API can be used to list all of the local virtual machine identifiers (which for the HotSpot VM map onto the JVM PID):</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">uk.co.cooljeff.dynamicattach</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.tools.attach.VirtualMachine</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.tools.attach.VirtualMachineDescriptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JVMFinder <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    List<span style="color: #339933;">&lt;</span>VirtualMachineDescriptor<span style="color: #339933;">&gt;</span> vmDescriptors = VirtualMachine.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>VirtualMachineDescriptor vmDescriptor : vmDescriptors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #003399; font-weight: bold;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Name: &quot;</span> + vmDescriptor.<span style="color: #006633;">displayName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                                                    + <span style="color: #0000ff;">&quot; PID: &quot;</span> + vmDescriptor.<span style="color: #006633;">id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>The above gives the following output on my machine (cut out a few for brevity):</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
Name: uk.co.cooljeff.dynamicattach.JVMFinder PID: <span style="color: #000000;">21116</span></pre>
<p>I&#8217;m about to spoil the fun now for those of you who were hoping to see some wacky OS dependent mechanism or hidden RMI registry for finding this information. The solution is quite simple, the JVM dumps a file containing performance data to a standard location which it uses to work out what JVMs are running on a host.</p>
<p>Specifically the performance data is for <a href="http://java.sun.com/performance/jvmstat/" target="_blank">jvmstat</a>, however it is this file that forms the basis of the Dynamic Attach implementation that HotSpot uses.</p>
<p>The following piece of code replicates what the LocalVmManager (part of jvmstat) does to locate running Java processes:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">uk.co.cooljeff.dynamicattach</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FilenameFilter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.regex.Matcher</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.regex.Pattern</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">sun.jvmstat.perfdata.monitor.protocol.local.PerfDataFile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JVMDataLocation <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Exception.html"><span style="color: #003399; font-weight: bold;">Exception</span></a> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html"><span style="color: #003399; font-weight: bold;">Pattern</span></a> filePattern = <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html"><span style="color: #003399; font-weight: bold;">Pattern</span></a>.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span>PerfDataFile.<span style="color: #006633;">userDirNamePattern</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">final</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html"><span style="color: #003399; font-weight: bold;">Matcher</span></a> fileMatcher = filePattern.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilenameFilter.html"><span style="color: #003399; font-weight: bold;">FilenameFilter</span></a> fileFilter = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilenameFilter.html"><span style="color: #003399; font-weight: bold;">FilenameFilter</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">boolean</span> accept<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #003399; font-weight: bold;">File</span></a> dir, <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        fileMatcher.<span style="color: #006633;">reset</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> fileMatcher.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #003399; font-weight: bold;">File</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> files = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #003399; font-weight: bold;">File</span></a><span style="color: #009900;">&#40;</span>PerfDataFile.<span style="color: #006633;">getTempDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">listFiles</span><span style="color: #009900;">&#40;</span>fileFilter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #003399; font-weight: bold;">File</span></a> file : files<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #003399; font-weight: bold;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PerfDataDir: &quot;</span> + file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Which gives the following on my machine:</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
PerfDataDir: <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hsperfdata_root
PerfDataDir: <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hsperfdata_tomcat55
PerfDataDir: <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hsperfdata_jeffsinc</pre>
<p>If you take a look at one of these directories you will find 1 file per Java process running by the specific user. The name of the file is the local virtual machine identifier (lvmid) which for HotSpot will correspond to the process id. If you run <em>strings</em> on the file you will see all of the properties relating to that JVM instance.</p>
<p>It is as simple as that, on Linux the JVM simply looks for all files that match: <em>/tmp/hsperfdata_*/*</em>.</p>
<p>Resources:</p>
<ul>
<li><a href="http://java.sun.com/javase/6/docs/technotes/guides/attach/index.html">Dynamic Attach API</a></li>
<li><a href="https://jdk7.dev.java.net">OpenJDK 7 source code</a></li>
<li><a href="https://visualvm.dev.java.net">Visual VM</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.cooljeff.co.uk/2009/04/05/detecting-all-running-jvms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
