<?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>Intrepid Blog &#187; reference counting</title>
	<atom:link href="http://blog.affien.com/archives/tag/reference-counting/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.affien.com</link>
	<description>A few thoughts</description>
	<lastBuildDate>Mon, 23 Jan 2012 08:47:37 +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>The GC: Reference Counter</title>
		<link>http://blog.affien.com/archives/2005/09/11/the-gc-reference-counter/</link>
		<comments>http://blog.affien.com/archives/2005/09/11/the-gc-reference-counter/#comments</comments>
		<pubDate>Sun, 11 Sep 2005 12:53:58 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[GC]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[reference counting]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/archives/2005/09/11/the-gc-reference-counter/</guid>
		<description><![CDATA[The simplest form of Garbage Collector is the Reference [...]]]></description>
			<content:encoded><![CDATA[<p>The simplest form of Garbage Collector is the Reference Counter Garbage Collector.</p>
<p>Each time an object is referenced (a pointer to it is made) the reference count on the object is incremented. If the pointer isn&#8217;t used anymore the reference count is decreased. If the reference count hits 0 the object will be freed.</p>
<p><a href="http://python.org/">Python</a> uses a Reference Counter.</p>
<h3>Advantages</h3>
<ul>
<li>A reference counter does its tracking at runtime. There is no big garbage collection, and the execution of the program won&#8217;t be interupted for a while when the garbage is collected.</li>
<li>It&#8217;s very easy to implement.</li>
</ul>
<h3>Disadvantages</h3>
<ul>
<li>Instead of the required <code>malloc</code> <code>free</code> pair required for each allocation without Gc, it now requires <code>add_ref</code> <code>free_ref</code> for each pointer/reference to an allocation. When someone forgets to release its reference the object persists.</li>
<li>Circular references will never be collected. An example of a circular reference is a list that contains as an item a reference to itself. When the list is released its count will remain 1 because inside the list it refers to itself and will therefore never be collected.</li>
<li>A RefCounter GC doesn&#8217;t keep track of pointers into the managed heap, and therefore can&#8217;t move any object because it`ll break pointers. This makes the fragmentation of the RefCounter GC as bad as <code>malloc</code>&#8216;s fragmentation.</li>
</ul>
<h3>Circular references</h3>
<p>Circular references can be fixed by adding a <em>trace</em> algorithm. This algorithm will trace through all objects from the root objects (pointers on the stack, etc.) and mark them. The objects that aren&#8217;t marked aren&#8217;t accessible and therefore are subject to Garbage Collection.</p>
<p>The problem is that a RefCounter Gc usually doesn&#8217;t keep track of what pointers are in objects, and that there could be pointers outside the managed heap which only use the <code>add_ref</code> and <code>free_ref</code>. Python struggled with this problem and it implemented the a trace algorithm that depended on the programmer&#8217;s of not-python-modules (of which pointers aren&#8217;t known) to implement the list of references to objects that also contain objects. By traversing links that way circular dependencies could be found without hurting unmanaged pointers, but this has a high performance hit on Python, and it is still adviced to avoid circular references.</p>
<h3>Conclusion</h3>
<p>RefCounter Gc&#8217;s are easy to implement, and a good idea for scripting languages which know all references so that circular references can be dealt with accoringly. But they still have a lot of disadvantages which other kinds of Gc&#8217;s solve a lot better, and more elegantly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2005/09/11/the-gc-reference-counter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

