<?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>Making Life Simple &#187; java</title>
	<atom:link href="http://www.lalitmehta.com/home/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lalitmehta.com/home</link>
	<description></description>
	<lastBuildDate>Tue, 09 Feb 2010 21:36:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Generate SSL certificate for Tomcat</title>
		<link>http://www.lalitmehta.com/home/2010/01/21/generate-ssl-certificate-for-tomcat/</link>
		<comments>http://www.lalitmehta.com/home/2010/01/21/generate-ssl-certificate-for-tomcat/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 18:21:00 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[general web]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/?p=144</guid>
		<description><![CDATA[Following are the steps to create an real SSL certificate (verified by VeriSign/Thawte etc.) for Tomcat: 1. Creation of keystore keytool -genkey -alias XXX -keyalg RSA -keystore ./XXX.keystore 2. Generation of CSR keytool -certreq -alias XXX -file XXX.csr -keystore ./XXX.keystore Send the generated XXX.csr to the signing authority and get the certificate from them. Save [...]]]></description>
			<content:encoded><![CDATA[<p>Following are the steps to create an real SSL certificate (verified by VeriSign/Thawte etc.) for Tomcat:<br />
1. Creation of keystore<br />
<code>keytool -genkey -alias XXX -keyalg RSA -keystore ./XXX.keystore</code></p>
<p>2. Generation of CSR<br />
<code>keytool -certreq -alias XXX -file XXX.csr -keystore ./XXX.keystore</code><br />
Send the generated XXX.csr to the signing authority and get the certificate from them.  Save that file as &#8220;XXX.thawte&#8221;</p>
<p>3. Install the certificate in the keystore<br />
<code>keytool -import -alias XXX -trustcacerts -file XXX.thawte  -keystore XXX.keystore</code></p>
<p>That&#8217;s it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2010/01/21/generate-ssl-certificate-for-tomcat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integrate Apache 2 with Tomcat 6</title>
		<link>http://www.lalitmehta.com/home/2010/01/18/integrate-apache-2-with-tomcat-6/</link>
		<comments>http://www.lalitmehta.com/home/2010/01/18/integrate-apache-2-with-tomcat-6/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 20:08:18 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[apache]]></category>
		<category><![CDATA[general web]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/?p=131</guid>
		<description><![CDATA[I have been trying to configure apache/tomcat is such a way that apache comes on the front-end so that I can have all the features of apache like URL rewriting, virtual hosts, PHP etc. and forward only certain requests to tomcat whose only job should be running servlets. I have CentOS installed on my server [...]]]></description>
			<content:encoded><![CDATA[<p>I have been trying to configure apache/tomcat is such a way that apache comes on the front-end so that I can have all the features of apache like URL rewriting, virtual hosts, PHP etc. and forward only certain requests to tomcat whose only job should be running servlets.</p>
<p>I have CentOS installed on my server with Apache 2 installed through yum and the goal was to install the binaries for Tomcat 6 and make them talk to each other.  After some googling, the best tutorial I found to configure tomcat through mod_jk was found <a href="http://www3.ntu.edu.sg/home/ehchua/programming/howto/Apache_Tomcat_HowTo.html" target="_blank">here</a>.  I have attached the PDF of the instructions <a title="Integrate Apache2 with Tomcat 6" href="http://www.lalitmehta.com/home/wp-content/uploads/2010/01/Apache2-Tomcat6.pdf" target="_blank">here</a>.<br />
After following the steps, I was easily able to create a worker and configure apache to forward the request to /examples to tomcat.<br />
The next issue was how to configure virtual hosts so that I can host multiple websites, some handled by apache and others through tomcat. Following is the snippet of httpd.conf setting that needs to be updated to enable virtual hosting.</p>
<p><code><br />
NameVirtualHost *:80<br />
#<br />
# NOTE: NameVirtualHost cannot be used without a port specifier<br />
# (e.g. :80) if mod_ssl is being used, due to the nature of the<br />
# SSL protocol.<br />
#<br />
#<br />
# VirtualHost example:<br />
# Almost any Apache directive may go into a VirtualHost container.<br />
# The first VirtualHost section is used for requests without a known<br />
# server name.<br />
#<br />
#&lt;VirtualHost *:80&gt;<br />
#    ServerAdmin webmaster@dummy-host.example.com<br />
#    DocumentRoot /www/docs/dummy-host.example.com<br />
#    ServerName dummy-host.example.com<br />
#    ErrorLog logs/dummy-host.example.com-error_log<br />
#    CustomLog logs/dummy-host.example.com-access_log common<br />
#&lt;/VirtualHost&gt;<br />
&lt;VirtualHost *:80&gt;</code><code><br />
ServerAdmin webmaster@lalitmehta.com<br />
ServerName www.lalitmehta.com<br />
ErrorLog logs/lalitmehta.com-error_log<br />
CustomLog logs/lalitmehta-access_log common<br />
&lt;/VirtualHost&gt;<br />
&lt;VirtualHost *:80&gt;<br />
ServerAdmin info@saiyam.com<br />
ServerName rsspile.com<br />
#   No need of DocumentRoot<br />
ErrorLog logs/rsspile.com-error_log<br />
CustomLog logs/rsspile.com-access_log common<br />
JkMount /rss ajp13<br />
JkMount /rss/* ajp13<br />
&lt;/VirtualHost&gt;<br />
</code><br />
<br />
I do have two questions though:<br />
1. since mod_jk.so is available for download for Linux, what advantage you get by compiling Apache and mod_jk from source? Most of the websites mention the need to compile the source. Let me know <img src='http://www.lalitmehta.com/home/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
2. How do I enable SSL on my server with the above configuration?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2010/01/18/integrate-apache-2-with-tomcat-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle connection with JDBC &#8211; JNDI</title>
		<link>http://www.lalitmehta.com/home/2009/01/14/oracle-connection-with-jdbc-jndi/</link>
		<comments>http://www.lalitmehta.com/home/2009/01/14/oracle-connection-with-jdbc-jndi/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 04:38:10 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[jndi]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/?p=70</guid>
		<description><![CDATA[Using JNDI javax.naming.Context context = new javax.naming.InitialContext(); javax.sql.DataSource ds =  (javax.sql.DataSource) context.lookup("java:comp/env/jdbc/XXXJNDINAMEXXX"); java.sql.Connection con = ds.getConnection();]]></description>
			<content:encoded><![CDATA[<ol>
<li> Using JNDI<code><br />
javax.naming.Context context = new </code><code>javax.naming.</code><code>InitialContext();<br />
javax.sql.DataSource ds =  (</code><code>javax.sql.DataSource</code><code>) context.lookup("java:comp/env/jdbc/XXXJNDINAMEXXX");<br />
java.sql.Connection con = ds.getConnection();</code></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2009/01/14/oracle-connection-with-jdbc-jndi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hibernate get distinct list with Criteria</title>
		<link>http://www.lalitmehta.com/home/2008/04/03/hibernate-get-distinct-list-with-criteria/</link>
		<comments>http://www.lalitmehta.com/home/2008/04/03/hibernate-get-distinct-list-with-criteria/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 21:38:41 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/2008/04/03/hibernate-get-distinct-list-with-criteria/</guid>
		<description><![CDATA[The only way I could figure out to get the distinct list using Hibernate Criteria is by using ResultTransformer function. eg. List result = session.createCriteria(Order.class) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;------- &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;.list(); The con is it fetches all the objects and then filters it. (very time consuming) The only way to circumvent this problem is to write query using [...]]]></description>
			<content:encoded><![CDATA[<p>The only way I could figure out to get the distinct list using Hibernate Criteria is by using ResultTransformer function.</p>
<p>eg.</p>
<p><code>List result = session.createCriteria(Order.class)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-------<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.list();<br />
</code></p>
<p>The con is it fetches all the objects and then filters it. (very time consuming)</p>
<p>The only way to circumvent this problem is to write query using HQL.</p>
<p>If you find a way to do that efficiently using Criteria, let me know <img src='http://www.lalitmehta.com/home/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2008/04/03/hibernate-get-distinct-list-with-criteria/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>java replaceAll error</title>
		<link>http://www.lalitmehta.com/home/2008/03/12/48/</link>
		<comments>http://www.lalitmehta.com/home/2008/03/12/48/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 21:40:09 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/2008/03/12/48/</guid>
		<description><![CDATA[public String replaceAll(String regex, String replacement) This method works fine if replacement string does not contain &#8216;$&#8217; or &#8216;/&#8217; characters.If replacement string contains these characters, the results can be in-accurate. Fix: Use Matcher.quoteReplacement(replacement) public String replaceAll(String regex, &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Matcher.quoteReplacement(replacement))]]></description>
			<content:encoded><![CDATA[<p><code>public String replaceAll(String regex, String replacement)</code></p>
<p>This method works fine if replacement string does not contain &#8216;$&#8217; or &#8216;/&#8217; characters.If replacement string contains these characters, the results can be in-accurate.</p>
<p><u>Fix</u>: Use Matcher.quoteReplacement(replacement)</p>
<p><code>public String replaceAll(String regex,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matcher.quoteReplacement(replacement))</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2008/03/12/48/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Secure file upload</title>
		<link>http://www.lalitmehta.com/home/2008/02/04/secure-file-upload/</link>
		<comments>http://www.lalitmehta.com/home/2008/02/04/secure-file-upload/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 02:26:38 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[general web]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/2008/02/04/secure-file-upload/</guid>
		<description><![CDATA[No matter what web based technology you are using,  these tips will always be helpful when it comes down to uploading files:  Keep uploaded files where they cannot be directly accessed by the users via a direct URL. This can be done either by storing uploaded files outside of the web root or configuring the [...]]]></description>
			<content:encoded><![CDATA[<p>No matter what web based technology you are using,  these tips will always be helpful when it comes down to uploading files:</p>
<ol>
<li> Keep uploaded files where they cannot be directly accessed by the users via a direct URL. This can be done either by storing uploaded files outside of the web root or configuring the web server to deny access to the uploads directory.</li>
<li>Use system-generated file names instead of the names supplied by users when storing files on the file system. This will prevent local file inclusion attacks and also make any kind of file name manipulation by the user impossible. More information can be found <a href="http://www.scanit.be/uploads/php-file-upload.pdf" target="_blank">here</a>.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2008/02/04/secure-file-upload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>java.net.SocketException: Too many open files</title>
		<link>http://www.lalitmehta.com/home/2008/01/18/javanetsocketexception-too-many-open-files/</link>
		<comments>http://www.lalitmehta.com/home/2008/01/18/javanetsocketexception-too-many-open-files/#comments</comments>
		<pubDate>Fri, 18 Jan 2008 22:27:29 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/2008/01/18/javanetsocketexception-too-many-open-files/</guid>
		<description><![CDATA[For System Wide settings To see the settings for maximum open files for the OS level, use following command. cat /proc/sys/fs/file-max This should be a value from 36000 to 50000 or more. To increase the system wide maximum open files, as root edit the /etc/sysctl.conf and add the following to the end of the file. [...]]]></description>
			<content:encoded><![CDATA[<p><span style="text-decoration: underline;">For System Wide settings</span><br />
To see the settings for maximum open files for the OS level, use following command.<br />
<em>cat /proc/sys/fs/file-max</em><br />
This should be a value from 36000 to 50000 or more. To increase the system wide maximum open files, as root edit the /etc/sysctl.conf and add the following to the end of the file.</p>
<p>Note: The following example will increase the maximum number of files to 49,500 on your currently running system and will persist after rebooting.</p>
<p>fs.file-max = 49500</p>
<p>Then issue the following command to activate this change to your live system.<br />
sysctl -p</p>
<p><span style="text-decoration: underline;">For user level setting</span></p>
<p>Also, you should update /etc/security/limits.conf for the user.</p>
<p>myuser hard nofile 2048<br />
myuser soft nofile 2048</p>
<p>Type this to see what it&#8217;s set at:</p>
<p>ulimit -a</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2008/01/18/javanetsocketexception-too-many-open-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Weak References</title>
		<link>http://www.lalitmehta.com/home/2007/10/24/weak-references/</link>
		<comments>http://www.lalitmehta.com/home/2007/10/24/weak-references/#comments</comments>
		<pubDate>Wed, 24 Oct 2007 19:51:52 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/2007/10/24/weak-references/</guid>
		<description><![CDATA[for original post go here. Strong references First I need to start with a refresher on strong references. A strong reference is an ordinary Java reference, the kind you use every day. For example, the code: StringBuffer buffer = new StringBuffer(); creates a new StringBuffer() and stores a strong reference to it in the variable [...]]]></description>
			<content:encoded><![CDATA[<p>for original post go <a href="http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html" title="Ethan Nicholas's Blog" target="_blank">here</a>.</p>
<p><strong>Strong references</strong></p>
<p>First I need to start with a refresher on strong references. A strong reference is an ordinary Java reference, the kind you use every day. For example, the code:</p>
<p><em>StringBuffer buffer = new StringBuffer();</em></p>
<p>creates a new StringBuffer() and stores a strong reference to it in the variable buffer. Yes, yes, this is kiddie stuff, but bear with me. The important part about strong references &#8212; the part that makes them &#8220;strong&#8221; &#8212; is how they interact with the garbage collector. Specifically, if an object is reachable via a chain of strong references (strongly reachable), it is not eligible for garbage collection. As you don&#8217;t want the garbage collector destroying objects you&#8217;re working on, this is normally exactly what you want.</p>
<p><strong>When strong references are too strong</strong></p>
<p>It&#8217;s not uncommon for an application to use classes that it can&#8217;t reasonably extend. The class might simply be marked final, or it could be something more complicated, such as an interface returned by a factory method backed by an unknown (and possibly even unknowable) number of concrete implementations. Suppose you have to use a class Widget and, for whatever reason, it isn&#8217;t possible or practical to extend Widget to add new functionality.</p>
<p>What happens when you need to keep track of extra information about the object? In this case, suppose we find ourselves needing to keep track of each Widget&#8217;s serial number, but the Widget class doesn&#8217;t actually have a serial number property &#8212; and because Widget isn&#8217;t extensible, we can&#8217;t add one. No problem at all, that&#8217;s what HashMaps are for:</p>
<p><em>serialNumberMap.put(widget, widgetSerialNumber);</em></p>
<p>This might look okay on the surface, but the strong reference to widget will almost certainly cause problems. We have to know (with 100% certainty) when a particular Widget&#8217;s serial number is no longer needed, so we can remove its entry from the map. Otherwise we&#8217;re going to have a memory leak (if we don&#8217;t remove Widgets when we should) or we&#8217;re going to inexplicably find ourselves missing serial numbers (if we remove Widgets that we&#8217;re still using). If these problems sound familiar, they should: they are exactly the problems that users of non-garbage-collected languages face when trying to manage memory, and we&#8217;re not supposed to have to worry about this in a more civilized language like Java.</p>
<p>Another common problem with strong references is caching, particular with very large structures like images. Suppose you have an application which has to work with user-supplied images, like the web site design tool I work on. Naturally you want to cache these images, because loading them from disk is very expensive and you want to avoid the possibility of having two copies of the (potentially gigantic) image in memory at once.</p>
<p>Because an image cache is supposed to prevent us from reloading images when we don&#8217;t absolutely need to, you will quickly realize that the cache should always contain a reference to any image which is already in memory. With ordinary strong references, though, that reference itself will force the image to remain in memory, which requires you (just as above) to somehow determine when the image is no longer needed in memory and remove it from the cache, so that it becomes eligible for garbage collection. Once again you are forced to duplicate the behavior of the garbage collector and manually determine whether or not an object should be in memory.</p>
<p><strong>Weak references</strong></p>
<p>A weak reference, simply put, is a reference that isn&#8217;t strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector&#8217;s ability to determine reachability for you, so you don&#8217;t have to do it yourself. You create a weak reference like this:</p>
<p><em>WeakReference weakWidget = new WeakReference(widget);</em></p>
<p>and then elsewhere in the code you can use weakWidget.get() to get the actual Widget object. Of course the weak reference isn&#8217;t strong enough to prevent garbage collection, so you may find (if there are no strong references to the widget) that weakWidget.get() suddenly starts returning null.</p>
<p>To solve the &#8220;widget serial number&#8221; problem above, the easiest thing to do is use the built-in WeakHashMap class. WeakHashMap works exactly like HashMap, except that the keys (not the values!) are referred to using weak references. If a WeakHashMap key becomes garbage, its entry is removed automatically. This avoids the pitfalls I described and requires no changes other than the switch from HashMap to a WeakHashMap. If you&#8217;re following the standard convention of referring to your maps via the Map interface, no other code needs to even be aware of the change.</p>
<p><strong>Reference queues<br />
</strong><br />
Once a WeakReference starts returning null, the object it pointed to has become garbage and the WeakReference object is pretty much useless. This generally means that some sort of cleanup is required; WeakHashMap, for example, has to remove such defunct entries to avoid holding onto an ever-increasing number of dead WeakReferences.</p>
<p>The ReferenceQueue class makes it easy to keep track of dead references. If you pass a ReferenceQueue into a weak reference&#8217;s constructor, the reference object will be automatically inserted into the reference queue when the object to which it pointed becomes garbage. You can then, at some regular interval, process the ReferenceQueue and perform whatever cleanup is needed for dead references.</p>
<p><strong>Different degrees of weakness</strong></p>
<p>Up to this point I&#8217;ve just been referring to &#8220;weak references&#8221;, but there are actually four different degrees of reference strength: strong, soft, weak, and phantom, in order from strongest to weakest. We&#8217;ve already discussed strong and weak references, so let&#8217;s take a look at the other two.</p>
<p><strong>Soft references</strong></p>
<p>A soft reference is exactly like a weak reference, except that it is less eager to throw away the object to which it refers. An object which is only weakly reachable (the strongest references to it are WeakReferences) will be discarded at the next garbage collection cycle, but an object which is softly reachable will generally stick around for a while.</p>
<p>SoftReferences aren&#8217;t required to behave any differently than WeakReferences, but in practice softly reachable objects are generally retained as long as memory is in plentiful supply. This makes them an excellent foundation for a cache, such as the image cache described above, since you can let the garbage collector worry about both how reachable the objects are (a strongly reachable object will never be removed from the cache) and how badly it needs the memory they are consuming.</p>
<p><strong>Phantom references</strong></p>
<p>A phantom reference is quite different than either SoftReference or WeakReference. Its grip on its object is so tenuous that you can&#8217;t even retrieve the object &#8212; its get() method always returns null. The only use for such a reference is keeping track of when it gets enqueued into a ReferenceQueue, as at that point you know the object to which it pointed is dead. How is that different from WeakReference, though?</p>
<p>The difference is in exactly when the enqueuing happens. WeakReferences are enqueued as soon as the object to which they point becomes weakly reachable. This is before finalization or garbage collection has actually happened; in theory the object could even be &#8220;resurrected&#8221; by an unorthodox finalize() method, but the WeakReference would remain dead. PhantomReferences are enqueued only when the object is physically removed from memory, and the get() method always returns null specifically to prevent you from being able to &#8220;resurrect&#8221; an almost-dead object.</p>
<p>What good are PhantomReferences? I&#8217;m only aware of two serious cases for them: first, they allow you to determine exactly when an object was removed from memory. They are in fact the only way to determine that. This isn&#8217;t generally that useful, but might come in handy in certain very specific circumstances like manipulating large images: if you know for sure that an image should be garbage collected, you can wait until it actually is before attempting to load the next image, and therefore make the dreaded OutOfMemoryError less likely.</p>
<p>Second, PhantomReferences avoid a fundamental problem with finalization: finalize() methods can &#8220;resurrect&#8221; objects by creating new strong references to them. So what, you say? Well, the problem is that an object which overrides finalize() must now be determined to be garbage in at least two separate garbage collection cycles in order to be collected. When the first cycle determines that it is garbage, it becomes eligible for finalization. Because of the (slim, but unfortunately real) possibility that the object was &#8220;resurrected&#8221; during finalization, the garbage collector has to run again before the object can actually be removed. And because finalization might not have happened in a timely fashion, an arbitrary number of garbage collection cycles might have happened while the object was waiting for finalization. This can mean serious delays in actually cleaning up garbage objects, and is why you can get OutOfMemoryErrors even when most of the heap is garbage.</p>
<p>With PhantomReference, this situation is impossible &#8212; when a PhantomReference is enqueued, there is absolutely no way to get a pointer to the now-dead object (which is good, because it isn&#8217;t in memory any longer). Because PhantomReference cannot be used to resurrect an object, the object can be instantly cleaned up during the first garbage collection cycle in which it is found to be phantomly reachable. You can then dispose whatever resources you need to at your convenience.</p>
<p>Arguably, the finalize() method should never have been provided in the first place. PhantomReferences are definitely safer and more efficient to use, and eliminating finalize() would have made parts of the VM considerably simpler. But, they&#8217;re also more work to implement, so I confess to still using finalize() most of the time. The good news is that at least you have a choice.</p>
<p>for original post go <a href="http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html" title="Ethan Nicholas's Blog" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2007/10/24/weak-references/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Zip a folder in Java</title>
		<link>http://www.lalitmehta.com/home/2007/09/18/how-to-zip-a-folder-in-java/</link>
		<comments>http://www.lalitmehta.com/home/2007/09/18/how-to-zip-a-folder-in-java/#comments</comments>
		<pubDate>Tue, 18 Sep 2007 22:27:58 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/2007/09/18/how-to-zip-a-folder-in-java/</guid>
		<description><![CDATA[Using the in-built zip function of java, attached is a sample code to copy a folder and all its sub-folders into a zip file. Java Program to Zip a folder This program uses Commons IO &#8211; IOUtils library to copy files.]]></description>
			<content:encoded><![CDATA[<p>Using the in-built zip function of java, attached is a sample code to copy a folder and all its sub-folders into a zip file.</p>
<p><a href="http://www.lalitmehta.com/home/wp-content/uploads/2007/09/zipfile.txt" target="_blank">Java Program to Zip a folder</a></p>
<p>This program uses Commons IO &#8211; <a href="http://commons.apache.org/io/" title="Commons IO" target="_blank">IOUtils</a> library to copy files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2007/09/18/how-to-zip-a-folder-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to check GMail RSS/Atom feed</title>
		<link>http://www.lalitmehta.com/home/2007/09/17/how-to-check-gmail-rssatom-feed/</link>
		<comments>http://www.lalitmehta.com/home/2007/09/17/how-to-check-gmail-rssatom-feed/#comments</comments>
		<pubDate>Mon, 17 Sep 2007 17:28:17 +0000</pubDate>
		<dc:creator>lalit</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[gmail]]></category>

		<guid isPermaLink="false">http://www.lalitmehta.com/home/2007/09/17/how-to-check-gmail-rssatom-feed/</guid>
		<description><![CDATA[Google provides a feed url to check your new mail(s) in your inbox. The url is https://gmail.google.com/gmail/feed/atom. You would need to authenticate first with your gmail username and password and it will list the unread email(s). I was reading about Rome and thought to write a small program utilizing its features to read my gmail [...]]]></description>
			<content:encoded><![CDATA[<p>Google provides a feed url to check your new mail(s) in your inbox. The url is https://gmail.google.com/gmail/feed/atom.</p>
<p>You  would need to authenticate first with your gmail username and password and it will list the unread email(s).</p>
<p>I was reading about <a href="https://rome.dev.java.net/" title="Rome RSS/ATOM Feed reader and writer" target="_blank">Rome</a> and thought to write a small program utilizing its features to read my <a href="http://www.gmail.com" title="Google Mail" target="_blank">gmail</a> feed. Attached is the java <a href="http://www.lalitmehta.com/home/wp-content/uploads/2007/09/checkmail.txt" title="Java Program to check gmail feed" target="_blank">program</a> to read the gmail feed.</p>
<p>Let me know if you have any comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalitmehta.com/home/2007/09/17/how-to-check-gmail-rssatom-feed/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
