<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shyam Sundar Choudhary on Medium]]></title>
        <description><![CDATA[Stories by Shyam Sundar Choudhary on Medium]]></description>
        <link>https://medium.com/@imshyam?source=rss-52731e73a908------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*T7n7NThGrJCnVPIv.jpg</url>
            <title>Stories by Shyam Sundar Choudhary on Medium</title>
            <link>https://medium.com/@imshyam?source=rss-52731e73a908------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 13:09:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@imshyam/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java References]]></title>
            <link>https://medium.com/@imshyam/java-references-120e437ddc9c?source=rss-52731e73a908------2</link>
            <guid isPermaLink="false">https://medium.com/p/120e437ddc9c</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Shyam Sundar Choudhary]]></dc:creator>
            <pubDate>Tue, 26 Feb 2019 18:15:44 GMT</pubDate>
            <atom:updated>2019-02-26T18:23:36.913Z</atom:updated>
            <content:encoded><![CDATA[<p>There are four types of references in Java:</p><ol><li>Strong Reference</li><li>Weak Reference</li><li>Soft Reference</li><li>Phantom Reference</li></ol><p>Below are the use cases and the definition of each reference.</p><h3><strong>Strong Reference</strong></h3><p>In Java, a <strong>Strong Reference</strong> is the default reference. These objects are not available for garbage collection. When an instance is not referenced by an object then it’s available for garbage collection.</p><pre>String str = new String(“Test”);</pre><h3><strong>Weak Reference</strong></h3><p><strong>Weak Reference</strong> doesn’t stop GC from collecting the object. When an object doesn’t have a strong or soft reference pointing to it that means it is either unreachable(So available to collect by GC) or weakly reachable(Again available to collect by GC).</p><p>To use Weak Reference:</p><pre>WeakReference&lt;T&gt; wr = new WeakReference&lt;T&gt;(objectOfT);</pre><p><strong>Use Cases:</strong></p><p>1. In Android, it is used if you want to update the UI from the Async task. Weak reference prevents the memory leak by allowing the object held by the reference to be garbage collected.</p><p>2. Another use case is a publisher and listener situation. The issue here arises when the listener can’t be unsubscribed from the publisher, therefore a strong reference is still available to a publisher and memory is leaked. In this case, we create a weak reference to an observer.</p><p>3. <strong>WeakHashMap</strong> is an implementation of Map interface, where every key is a weak reference to the given key.</p><h3><strong>Soft Reference</strong></h3><p>A reference that is not garbage collected until JVM needs the memory badly is called a <strong>Soft reference</strong>. This memory is freed only when JVM runs out of memory and throw <em>OutOfMemoryError</em>.</p><p>To use Soft Reference:</p><pre>SoftReference&lt;T&gt; sr = new SoftReference&lt;T&gt;(objectOfT);</pre><p>Soft Reference objects are available for some time after the object creation. This time can be changed by <em>SoftRefLRUPolicyMSPerMB</em> flag:</p><pre>-XX:SoftRefLRUPolicyMSPerMB=2000</pre><p>Soft Reference has a longer lifetime than Weak Reference and continues to exist until extra memory is required.</p><p><strong>Use Case:</strong></p><p>1. It can be used to create a memory-sensitive cache. As long as the soft reference is in use, it works as a strong reference and can’t be garbage collected.</p><h3><strong>Phantom Reference</strong></h3><p><strong>Phantom References</strong> are available for garbage collection but before removing the object from memory, JVM puts it into a queue called <em>reference queue</em>. They are put in reference queue after calling <strong><em>finalize()</em></strong> method of its referent is executed.</p><p>The referent of a phantom reference is not available, means nothing refers to the phantom reference. That’s why we need a reference queue to work them.</p><p><strong>Use Case:</strong></p><ol><li>Phantom references can be used to check when an object was removed from Memory. This helps in scheduling memory sensitive tasks. Like, you can wait for a large object to be removed from memory before loading a new one.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=120e437ddc9c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>