<?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>Tewha &#187; memory management</title>
	<atom:link href="http://tewha.net/tag/memory-management/feed/" rel="self" type="application/rss+xml" />
	<link>http://tewha.net</link>
	<description>Writings and links on iPhone and iPad programming</description>
	<lastBuildDate>Sat, 04 Feb 2012 06:19:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Objective-C memory management</title>
		<link>http://tewha.net/2011/08/objective-c-memory-management/</link>
		<comments>http://tewha.net/2011/08/objective-c-memory-management/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 21:34:37 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[memory management]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=1232</guid>
		<description><![CDATA[With Automatic Reference Counting (ARC) coming out soon, you could argue this post is coming almost too late. But there&#8217;s a lot of confusion over this, and I don&#8217;t think ARC will help much if you don&#8217;t understand the why &#8230; <a href="http://tewha.net/2011/08/objective-c-memory-management/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>With Automatic Reference Counting (ARC) coming out soon, you could argue this post is coming almost too late. But there&#8217;s a lot of confusion over this, and I don&#8217;t think ARC will help much if you don&#8217;t understand the <em>why</em> of memory management.</p>

<p>After a couple years, I&#8217;ve come to adopt some very simple rules for memory management.</p>

<h2>Concepts</h2>

<p>The first step is to pick up a few concepts that will help you as you code.</p>

<ul>
<li><strong>Be lazy in memory management.</strong> Rely on the compiler to do everything it possibly can. The compiler can synthesize property setters that handle memory management according to the rules you set. Rely on this.</li>
<li><strong>Ownership.</strong> Think in terms of ownership, rather than reference counting. Thinking in terms of ownership makes problems and solutions obvious. Thinking in terms of ownership will help you keep in mind the <em>why</em> of what you&#8217;re doing, rather than the <em>what</em>. After you&#8217;ve got a good grasp of <em>why</em>, the <em>what</em> becomes obvious.</li>
<li><strong>Protect your objects.</strong> Own any object that you want to keep around as long as <code>self</code> is around.</li>
<li><strong>Avoid circular references.</strong> Never own an object that could own <code>self</code>. This means never owning <code>self</code>&#8216;s delegate or data source.</li>
</ul>

<h2>Practical guidelines</h2>

<p>But how does this translate to code? There&#8217;s a few specific and concrete things you can do to make this easier on yourself:</p>

<ul>
<li><strong>Use properties to centralize memory management rules.</strong> Use <code>@property</code> to establish the rules for memory management of an instance variable. (And <code>@synthesize</code> your properties to underscored instance variables to prevent naming conflicts.)</li>
<li><strong>Pick the right memory management rule.</strong> Use <code>retain</code> properties as your default. Data sources, delegates or other objects that might own the object you&#8217;re working in should be <code>assign</code> properties instead of <code>retain</code>.</li>
<li><strong>Rely on the centralized memory management rules.</strong> Use property set notation (<code>self.foo = nil;</code>) or the property setter (<code>[self setFoo: nil];</code>) everywhere you change the property&#8217;s value except in the property&#8217;s setter (if you need one) In this way, you rely on the <code>retain</code> or <code>assign</code> in your <code>@property</code> to specify the memory management. If you need to use <code>retain</code>, <code>release</code> or <code>autorelease</code> outside of a setter you&#8217;re doing too much. (And, also, you&#8217;ll have more work to do to be compatible with ARC.)</li>
<li><strong>Avoid being called once deallocated.</strong> In <code>dealloc</code>, <code>nil</code> any delegates you&#8217;ve set to <code>self</code>. (This eliminates the possibility that the object will survive longer than <code>self</code> and try to send messages to <code>self</code>.)</li>
<li><strong>Protect your private bits, too.</strong> Don&#8217;t use raw instance variables in objects requiring memory management. Use anonymous properties.</li>
<li><strong>Containers own their contents.</strong> You not only don&#8217;t need to but must not try to manage memory in a system container. An <code>NSArray</code> will send a <code>retain</code> to objects that are added to it, and a <code>release</code> to objects as they are removed. If you try to manually <code>retain</code> and <code>release</code> objects, you&#8217;ll miss a case.</li>
<li><strong>Verify.</strong> Analyze your code frequently. And profile your code early and often for leaks.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2011/08/objective-c-memory-management/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Assign, retain, copy pitfalls in Objective-C</title>
		<link>http://tewha.net/2010/06/assign-retain-copy-pitfalls-in-objective-c/</link>
		<comments>http://tewha.net/2010/06/assign-retain-copy-pitfalls-in-objective-c/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 15:11:47 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[iOS Development]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[properties]]></category>

		<guid isPermaLink="false">http://tewha.net/2010/06/assign-retain-copy-pitfalls-in-objective-c/</guid>
		<description><![CDATA[Cocoa With Love (Matt Gallagher): Assign, retain, copy pitfalls in Objective-C.]]></description>
			<content:encoded><![CDATA[<p>Cocoa With Love (Matt Gallagher): <a href="http://feedproxy.google.com/~r/CocoaWithLove/~3/d2Zoneyryec/assign-retain-copy-pitfalls-in-obj-c.html">Assign, retain, copy pitfalls in Objective-C</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2010/06/assign-retain-copy-pitfalls-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory and thread-safe custom property methods</title>
		<link>http://tewha.net/2009/10/memory-and-thread-safe-custom-property-methods/</link>
		<comments>http://tewha.net/2009/10/memory-and-thread-safe-custom-property-methods/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 19:28:50 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[properties]]></category>
		<category><![CDATA[threadsafe]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=1079</guid>
		<description><![CDATA[Cocoa With Love (Matt Gallagher): Memory and thread-safe custom property methods]]></description>
			<content:encoded><![CDATA[<p>Cocoa With Love (Matt Gallagher): <a href="http://cocoawithlove.com/2009/10/memory-and-thread-safe-custom-property.html">Memory and thread-safe custom property methods</a></p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2009/10/memory-and-thread-safe-custom-property-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rules to avoid retain cycles</title>
		<link>http://tewha.net/2009/07/rules-to-avoid-retain-cycles/</link>
		<comments>http://tewha.net/2009/07/rules-to-avoid-retain-cycles/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 01:16:56 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[release retain]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=967</guid>
		<description><![CDATA[Matt Gallagher: Rules to avoid retain cycles]]></description>
			<content:encoded><![CDATA[<p>Matt Gallagher: <a href="http://cocoawithlove.com/2009/07/rules-to-avoid-retain-cycles.html">Rules to avoid retain cycles</a></p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2009/07/rules-to-avoid-retain-cycles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keeping optimal autorelease pools</title>
		<link>http://tewha.net/2009/04/keeping-optimal-autorelease-pools/</link>
		<comments>http://tewha.net/2009/04/keeping-optimal-autorelease-pools/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 17:40:05 +0000</pubDate>
		<dc:creator>Steven Fisher</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[autorelease]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://tewha.net/?p=833</guid>
		<description><![CDATA[Martin Pilkington on autorelease pools: However, a problem arises when you&#8217;re creating a lot of objects at once. The obvious solution is to initialise and release objects by hand in this case, but sometimes it isn&#8217;t possible. A lot of &#8230; <a href="http://tewha.net/2009/04/keeping-optimal-autorelease-pools/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Martin Pilkington <a href="http://www.mcubedsw.com/blog/index.php?/site/comments/lazing_by_the_nsautoreleasepool/">on autorelease pools</a>:</p>

<blockquote>However, a problem arises when you&#8217;re creating a lot of objects at once. The obvious solution is to initialise and release objects by hand in this case, but sometimes it isn&#8217;t possible. A lot of objects returned by Cocoa methods are autoreleased (by convention any object returned by a class method (other than +new or +alloc) should be autoreleased).

</blockquote>

<p>This is a good practical example of autorelease pool manipulation, including numbers showing before and after.</p>
]]></content:encoded>
			<wfw:commentRss>http://tewha.net/2009/04/keeping-optimal-autorelease-pools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: tewha.net @ 2012-02-08 14:54:31 -->
