<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>BlackBerry Developer Blog &#187; weak-reference</title>
	<atom:link href="http://devblog.blackberry.com/tag/weak-reference/feed/" rel="self" type="application/rss+xml" />
	<link>http://devblog.blackberry.com</link>
	<description></description>
	<lastBuildDate>Wed, 22 May 2013 19:49:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='devblog.blackberry.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/9ef0a66c09615fa946c4179662398878?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>BlackBerry Developer Blog &#187; weak-reference</title>
		<link>http://devblog.blackberry.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://devblog.blackberry.com/osd.xml" title="BlackBerry Developer Blog" />
	<atom:link rel='hub' href='http://devblog.blackberry.com/?pushpress=hub'/>
		<item>
		<title>How To Find That Memory Leak (Part Three): Why An Object is Leaking Into Memory</title>
		<link>http://devblog.blackberry.com/2010/03/how-to-find-that-memory-leak-part-three/</link>
		<comments>http://devblog.blackberry.com/2010/03/how-to-find-that-memory-leak-part-three/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 10:00:00 +0000</pubDate>
		<dc:creator>Kamen V.</dc:creator>
				<category><![CDATA[How-to]]></category>
		<category><![CDATA[Java Development]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[memory leak]]></category>
		<category><![CDATA[virtual machine]]></category>
		<category><![CDATA[weak-reference]]></category>

		<guid isPermaLink="false">http://devblog.blackberry.com/?p=931</guid>
		<description><![CDATA[Investigating the cause of application memory leaks on BlackBerry smartphones. Part three in a three part series. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devblog.blackberry.com&#038;blog=17235680&#038;post=931&#038;subd=rimdevblog&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-982" title="Hunting a memory leak" src="http://rimdevblog.files.wordpress.com/2010/03/blackberry-memory-leak.jpg?w=558&#038;h=416" alt="" width="558" height="416" /></p>
<p>This is the third of a three-part series on how to detect a memory leak.  In <a title="Find That Memory Leak - Part One" href="http://rimdevblog.files.wordpress.com/2010/03/blackberry-memory-leak-mobile.png2009/10/how-to-find-that-memory-leak-part-one/">part one</a>, I explained the most common cases for a memory leak in a Java® development environment, and how that can affect your application. In <a title="Find That Memory Leak - Part Two" href="http://rimdevblog.files.wordpress.com/2010/03/blackberry-memory-leak-mobile.png2010/01/how-to-find-that-memory-leak-part-two-detecting-the-leak/">part two</a>, I discussed how to detect potential leaks. Today, we’re going to investigate why an object is leaking into memory in the first place.</p>
<p><span id="more-931"></span></p>
<h2>Leak investigation</h2>
<p>Once you&#8217;ve identified the class name of the object that is leaking, open the object&#8217;s view &#8211; View/Objects. This view allows viewing all class instances currently on the BlackBerry® smartphone. Before proceeding, press the &#8220;GC&#8221; button to clean any unreferenced instances (you may have done this already in the Object Statistics view, but doing it again doesn’t hurt). Starting with the BlackBerry® Java® Development Environment (BlackBerry JDE) v4.6, you can take advantage of a convenient functionality that lets you show all recursive references to an instance. <strong>So if you&#8217;re using BlackBerry JDE 4.6 or newer, follow the set of instructions below; otherwise look at the second set of instructions.</strong></p>
<ol>
<li>In the &#8220;Type:&#8221; field, enter the name of the class you&#8217;re after &#8211; e.g. &#8220;TestMemoryLeaks&#8221; &#8211; and press enter.  You will get a list of instances &#8211; these are the actual &#8220;leaks&#8221;.</li>
<li>Pick one of those instances in the objects view, right-click and choose “Show Recursive References”. This will yield a very large list of objects (everything that references your object, directly or indirectly).</li>
<li>In the “Location” dropdown, select “Root Objects”. This will restrict the view to root references only (static variable, persistent root, app registry, local variables, etc).</li>
<li>One of the instances in the list will be of a class called ApplicationRegistryHashtable. Right click on it and choose “Show paths from @xxxxxx to @yyyyy”.</li>
<li>Now you have the reference chain that causes the leak.</li>
</ol>
<p><strong>If you have a version of the BlackBerry JDE older than 4.6, use the following steps:</strong></p>
<ol>
<li>In the &#8220;Type:&#8221; field, enter the name of the class you&#8217;re after &#8211; e.g. &#8220;TestMemoryLeaks&#8221; &#8211; and press enter.  You will get a list of instances &#8211; these are the actual &#8220;leaks&#8221;.</li>
<li>Pick one of those instances in the objects view, right-click and choose “Show References to @xxxxxxx”. This will give you a list of immediate references.</li>
<li>Check if one of the instances in the list is not ApplicationRegistryHashtable. If it is then go to step 6.</li>
<li>Right-click on any item in the new list and select &#8220;Show References To This Object List&#8221;. This will give you another list.</li>
<li>Repeat steps 3 and 4 until you reach an instance of ApplicationRegistryHashtable.</li>
<li>Right-click on ApplicationRegistryHashtable and choose “Show paths from @xxxxxx to @yyyyy”.</li>
<li>Now you have the reference chain that causes the leak.</li>
</ol>
<p>Having the reference chain to the leaked object instance should provide you with enough information about what caused the problem, and to avoid it.</p>
<p>So enough reading, hunters &#8211; minimize this browser and go back to your BlackBerry JDE to start the memory leak hunt! After that, come back and share your experience with everyone by posting a comment. Have you found any leaks? What were the core reasons for having the leaks on the first place? Let us all know.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rimdevblog.wordpress.com/931/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rimdevblog.wordpress.com/931/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devblog.blackberry.com&#038;blog=17235680&#038;post=931&#038;subd=rimdevblog&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devblog.blackberry.com/2010/03/how-to-find-that-memory-leak-part-three/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/832dccdac0a9f32971905af91e11034e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kamenv1</media:title>
		</media:content>

		<media:content url="http://rimdevblog.files.wordpress.com/2010/03/blackberry-memory-leak.jpg" medium="image">
			<media:title type="html">Hunting a memory leak</media:title>
		</media:content>
	</item>
		<item>
		<title>BlackBerry API Hidden Gems (Part One)</title>
		<link>http://devblog.blackberry.com/2009/08/blackberry-api-hidden-gems-part-one/</link>
		<comments>http://devblog.blackberry.com/2009/08/blackberry-api-hidden-gems-part-one/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 12:27:00 +0000</pubDate>
		<dc:creator>Denver C.</dc:creator>
				<category><![CDATA[Java Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[weak-reference]]></category>

		<guid isPermaLink="false">http://devblog.blackberry.com/?p=836</guid>
		<description><![CDATA[Every time I think I know everything, I am always proven wrong.  For example, I used to think I knew everything about BlackBerry® APIs but as I continue developing software I am still finding new, cool classes and methods that make my life easier. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devblog.blackberry.com&#038;blog=17235680&#038;post=836&#038;subd=rimdevblog&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p style="text-align:center;"><img class="aligncenter size-full wp-image-837" title="hidden gems" src="http://rimdevblog.files.wordpress.com/2009/08/hidden-gems.jpg?w=480&#038;h=360" alt="" width="480" height="360" /></p>
<p>Every time I think I know everything, I am always proven wrong.  For example, I used to think I knew everything about BlackBerry® APIs but as I continue developing software I am still finding new, cool classes and methods that make my life easier.  I call them “hidden gems” since they are often found by one stumbling across them, but when found are of great use.  In this post I will highlight some of my favourite gems, which I hope will make your life as a developer easier too.  So let’s dive in.</p>
<h2>Weak References</h2>
<p>For the longest time I had heard the term “weak references” but never really knew what they were.  Once I finally learned, I started to use them all the time to be more efficient and avoid memory leaks.  Indeed, weak references are not BlackBerry specific, but are defined right in the CLDC specification in the class java.lang.ref.WeakReference.</p>
<p>So what is a “weak reference” anyways?  The behaviour of a weak reference when contrasted with the normal “strong” reference pertains to making an object a candidate for garbage collection. A weak reference gives you the ability to access an object but also makes it available to the garbage collector.  So why would this be useful?  I find that there are often circumstances when you only care about an object if someone else does.  Holding a weak reference to an object is one way of determining if anyone else cares about it, based on the assumption that anyone who cares about it will hold a strong reference to it.  Once nobody cares about the object (other than me) it will be garbage collected.  This is the opposite of holding a strong reference to the object, as doing so would prevent the object from being garbage collected, wasting precious memory.</p>
<p>One excellent use case for weak references is holding a reference to an application object.  In general, you only need to reference an application object while the application it represents is alive; once the application exits you no longer need the reference. By holding a strong reference to the application object, the garbage collector will be prevented from freeing the object’s memory even after the application exits, along with the massive number of objects that it recursively references. Instead, holding a weak reference to the application object allows it to be garbage collected when the application exits.</p>
<p>Below is an example of how a weak reference to an application object can be used in a memory-efficient manner to determine if an application is still alive.</p>
<div class="sample"><code>import net.rim.device.api.system.Application;<br />
import java.lang.ref.WeakReference;</code></p>
<p>public class ApplicationLifeMonitor {</p>
<p>private WeakReference _appRef;</p>
<p>public ApplicationLifeMonitor() {<br />
Application app = Application.getApplication();<br />
_appRef = new WeakReference(app);<br />
}</p>
<p>public boolean isAlive() {<br />
Application app = (Application) _appRef.get();<br />
return (app != null &amp;&amp; app.isAlive());<br />
}<br />
}</p>
</div>
<h2>UIDGenerator</h2>
<p>The UIDGenerator class does exactly what its name implies: generates unique IDs.  Its class javadocs defines it as “a utility class to generate 32-bit unique IDs that can be used for synchronization or any other purpose.” Here’s an example to show how it could be used:</p>
<div class="sample">
<p><code>public class Person {</code></p>
<p>private int _id;<br />
private String _name;</p>
<p>public Person(String name) {<br />
this._id = UIDGenerator.getUID();<br />
this._name = name;<br />
}</p>
<p>public int getId() {<br />
return this._id;<br />
}</p>
<p>public String getName() {<br />
return this._name;<br />
}<br />
}</p>
</div>
<h2>IOUtilities.streamToBytes()</h2>
<p>Have you ever written code like this to dump the bytes from an InputStream into a byte[] array?</p>
<div class="sample"><code>public static byte[] getBytes(InputStream in)<br />
throws IOException {<br />
ByteArrayOutputStream out = new ByteArrayOutputStream();<br />
byte[] buffer = new byte[2048];<br />
int readCount = in.read(buffer);<br />
while (readCount &gt;= 0) {<br />
out.write(buffer, 0, readCount);<br />
readCount = in.read(buffer);<br />
}<br />
return out.toByteArray();<br />
}<br />
</code></div>
<p>Now you can do it in one line:</p>
<div class="sample"><code>public static byte[] getBytes(InputStream in)<br />
throws IOException {<br />
return net.rim.device.api.io.IOUtilities.streamToBytes(in);<br />
}<br />
</code></div>
<p>Impress your friends at your next party with this neat trick.</p>
<p>Stay tuned for <a href="http://rimdevblog.files.wordpress.com/2009/08/hidden-gems.jpg2009/08/blackberry-api-hidden-gems-part-two/">tomorrow&#8217;s post</a> when I&#8217;ll share even more BlackBerry API gems with you!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rimdevblog.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rimdevblog.wordpress.com/836/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devblog.blackberry.com&#038;blog=17235680&#038;post=836&#038;subd=rimdevblog&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devblog.blackberry.com/2009/08/blackberry-api-hidden-gems-part-one/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/035dc37bb725cb59d3b846aeb8a55c6c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">denvercon1</media:title>
		</media:content>

		<media:content url="http://rimdevblog.files.wordpress.com/2009/08/hidden-gems.jpg" medium="image">
			<media:title type="html">hidden gems</media:title>
		</media:content>
	</item>
	</channel>
</rss>
