<?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; hotspot</title>
	<atom:link href="http://www.cooljeff.co.uk/tag/hotspot/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>270</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>

