<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: [.NET Internals 07] Unmanaged resources: finalization, fReachable queue and dispose pattern	</title>
	<atom:link href="https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/</link>
	<description>Building real-world software with AI 🤖</description>
	<lastBuildDate>Sat, 17 Nov 2018 12:08:43 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>
	<item>
		<title>
		By: Dawid Sibiński		</title>
		<link>https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-150</link>

		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Sun, 09 Sep 2018 18:28:19 +0000</pubDate>
		<guid isPermaLink="false">https://www.dsibinski.pl/?p=2779#comment-150</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-149&quot;&gt;Wojtek Grzesiak&lt;/a&gt;.

You&#039;re right, it&#039;s included in the Quick Actions ;) Thanks, didn&#039;t know this one!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-149">Wojtek Grzesiak</a>.</p>
<p>You&#8217;re right, it&#8217;s included in the Quick Actions 😉 Thanks, didn&#8217;t know this one!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Wojtek Grzesiak		</title>
		<link>https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-149</link>

		<dc:creator><![CDATA[Wojtek Grzesiak]]></dc:creator>
		<pubDate>Sat, 08 Sep 2018 08:26:10 +0000</pubDate>
		<guid isPermaLink="false">https://www.dsibinski.pl/?p=2779#comment-149</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-141&quot;&gt;Dawid Sibiński&lt;/a&gt;.

Code snippet is provided probably in the newest VS, I discovered it after I read your post :D
 https://uploads.disquscdn.com/images/4291fb8a8c917ae048fd65877302645419ec403ef5c45434d1e2491a30e1f70b.png]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-141">Dawid Sibiński</a>.</p>
<p>Code snippet is provided probably in the newest VS, I discovered it after I read your post 😀<br />
 <a href="https://uploads.disquscdn.com/images/4291fb8a8c917ae048fd65877302645419ec403ef5c45434d1e2491a30e1f70b.png" rel="nofollow ugc">https://uploads.disquscdn.com/images/4291fb8a8c917ae048fd65877302645419ec403ef5c45434d1e2491a30e1f70b.png</a></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dawid Sibiński		</title>
		<link>https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-147</link>

		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Fri, 07 Sep 2018 20:42:15 +0000</pubDate>
		<guid isPermaLink="false">https://www.dsibinski.pl/?p=2779#comment-147</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-145&quot;&gt;Konrad Kokosa&lt;/a&gt;.

Thanks for clarifications Konrad ;)
Hope it&#039;s clearer now for Jim and everyone else interested.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-145">Konrad Kokosa</a>.</p>
<p>Thanks for clarifications Konrad 😉<br />
Hope it&#8217;s clearer now for Jim and everyone else interested.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Konrad Kokosa		</title>
		<link>https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-145</link>

		<dc:creator><![CDATA[Konrad Kokosa]]></dc:creator>
		<pubDate>Thu, 06 Sep 2018 08:01:14 +0000</pubDate>
		<guid isPermaLink="false">https://www.dsibinski.pl/?p=2779#comment-145</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-144&quot;&gt;Dawid Sibiński&lt;/a&gt;.

`GC.SuppressFinalize` is designed to be called from user threads so its overhead needs to be as low as possible. Thus, the only thing it does is what documentation says - it sets single bit in an object header (look for `BIT_SBLK_FINALIZER_RUN` in CoreCLR if you are really interested). Any manipulation on finalization/fReachable queues would require synchronization between threads, the overhead of which we want to avoid.

This bit is then checked by the finalization thread but only for objects in finalization queue that just become unreachable  - if set, it simply removes the object from the finalization queue instead of promoting it to fReachable queue. 

The only way to call `GC.SuppressFinalize` on an object in fReachable queue is from its `Finalize` method which is obviously too late (finalization is just happenning). Bit would be set but it doesn&#039;t matter as there is nothing that would interpret it afterwards.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-144">Dawid Sibiński</a>.</p>
<p>`GC.SuppressFinalize` is designed to be called from user threads so its overhead needs to be as low as possible. Thus, the only thing it does is what documentation says &#8211; it sets single bit in an object header (look for `BIT_SBLK_FINALIZER_RUN` in CoreCLR if you are really interested). Any manipulation on finalization/fReachable queues would require synchronization between threads, the overhead of which we want to avoid.</p>
<p>This bit is then checked by the finalization thread but only for objects in finalization queue that just become unreachable  &#8211; if set, it simply removes the object from the finalization queue instead of promoting it to fReachable queue. </p>
<p>The only way to call `GC.SuppressFinalize` on an object in fReachable queue is from its `Finalize` method which is obviously too late (finalization is just happenning). Bit would be set but it doesn&#8217;t matter as there is nothing that would interpret it afterwards.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dawid Sibiński		</title>
		<link>https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-144</link>

		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 05 Sep 2018 17:58:16 +0000</pubDate>
		<guid isPermaLink="false">https://www.dsibinski.pl/?p=2779#comment-144</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-142&quot;&gt;Jim Przybylinski&lt;/a&gt;.

Hey Jim,
good point.
Microsoft docs says: &quot;This method sets a bit in the object header of obj, which the runtime checks when calling finalizers&quot;

However if you think about it, in my implementation GC.SuppressFinalize(this) can be called either directly from code (when the object is still referenced by something, meaning the reference is present on the finalization queue) or by the &quot;finalization thread&quot;, when the reference will be already moved to fReachable queue. So I guess in fact it removes it either from the finalization queue or from fReachable queue, depending when the method is called.

On the other hand I don&#039;t find any more details on a bit flag mentioned in Microsoft docs. The &quot;GC cycle&quot; is passed anyway - as I wrote &quot;finalizable objects stay uncollected for at least 1 more GC round than “normal” objects&quot;.

Anyway, the goal of calling GC.SuppressFinalize is to prevent your object to be finalized twice.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-142">Jim Przybylinski</a>.</p>
<p>Hey Jim,<br />
good point.<br />
Microsoft docs says: &#8220;This method sets a bit in the object header of obj, which the runtime checks when calling finalizers&#8221;</p>
<p>However if you think about it, in my implementation GC.SuppressFinalize(this) can be called either directly from code (when the object is still referenced by something, meaning the reference is present on the finalization queue) or by the &#8220;finalization thread&#8221;, when the reference will be already moved to fReachable queue. So I guess in fact it removes it either from the finalization queue or from fReachable queue, depending when the method is called.</p>
<p>On the other hand I don&#8217;t find any more details on a bit flag mentioned in Microsoft docs. The &#8220;GC cycle&#8221; is passed anyway &#8211; as I wrote &#8220;finalizable objects stay uncollected for at least 1 more GC round than “normal” objects&#8221;.</p>
<p>Anyway, the goal of calling GC.SuppressFinalize is to prevent your object to be finalized twice.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jim Przybylinski		</title>
		<link>https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-142</link>

		<dc:creator><![CDATA[Jim Przybylinski]]></dc:creator>
		<pubDate>Wed, 05 Sep 2018 17:01:22 +0000</pubDate>
		<guid isPermaLink="false">https://www.dsibinski.pl/?p=2779#comment-142</guid>

					<description><![CDATA[&#062; The framework also provides a GC.SuppressFinalize method which tells GC that the object has been manually disposed and no more finalization is necessary. As soon as it’s called, the object reference is removed from fReachable queue.

Is the object reference removed from the fReachable queue or the finalization queue, or both? Removing it from the fReachable queue doesn&#039;t seem all that helpful, as the first opportunity to be GCed has already passed.]]></description>
			<content:encoded><![CDATA[<p>&gt; The framework also provides a GC.SuppressFinalize method which tells GC that the object has been manually disposed and no more finalization is necessary. As soon as it’s called, the object reference is removed from fReachable queue.</p>
<p>Is the object reference removed from the fReachable queue or the finalization queue, or both? Removing it from the fReachable queue doesn&#8217;t seem all that helpful, as the first opportunity to be GCed has already passed.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dawid Sibiński		</title>
		<link>https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-141</link>

		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 05 Sep 2018 15:19:19 +0000</pubDate>
		<guid isPermaLink="false">https://www.dsibinski.pl/?p=2779#comment-141</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-140&quot;&gt;Wojtek Grzesiak&lt;/a&gt;.

Hey Wojtek,
thanks for your comment! :)
As you know, Dispose is just a pattern and there may be multiple implementations, each one probably better for different use cases (it depends for example is your class is sealed or not). However, I&#039;ve added the link you mentioned into the post so everyone can take a look on this implementation. I guess Microsoft contributors are worth trusting ;) Thanks for pointing it out!

Can you also direct us to the VS snippet? What&#039;s the name of it and how to use it?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-140">Wojtek Grzesiak</a>.</p>
<p>Hey Wojtek,<br />
thanks for your comment! 🙂<br />
As you know, Dispose is just a pattern and there may be multiple implementations, each one probably better for different use cases (it depends for example is your class is sealed or not). However, I&#8217;ve added the link you mentioned into the post so everyone can take a look on this implementation. I guess Microsoft contributors are worth trusting 😉 Thanks for pointing it out!</p>
<p>Can you also direct us to the VS snippet? What&#8217;s the name of it and how to use it?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Wojtek Grzesiak		</title>
		<link>https://www.codejourney.net/net-internals-07-unmanaged-resources-finalization-freachable-queue-and-dispose-pattern/#comment-140</link>

		<dc:creator><![CDATA[Wojtek Grzesiak]]></dc:creator>
		<pubDate>Wed, 05 Sep 2018 15:09:19 +0000</pubDate>
		<guid isPermaLink="false">https://www.dsibinski.pl/?p=2779#comment-140</guid>

					<description><![CDATA[Hej Dawid,
Good Article, I would add also that microsoft has some recomendations in terms of implementation of IDisposable interface https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose. It is almost just the naming difference, instead of CleanUp method You should probably have protected virtual void Dispose(bool disposing) and some additional flag to check if the object has not been already disposed. In the newest visual studio there is nice code snippet that provides some sample implementation:) Anyway good job!]]></description>
			<content:encoded><![CDATA[<p>Hej Dawid,<br />
Good Article, I would add also that microsoft has some recomendations in terms of implementation of IDisposable interface <a href="https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose" rel="nofollow ugc">https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose</a>. It is almost just the naming difference, instead of CleanUp method You should probably have protected virtual void Dispose(bool disposing) and some additional flag to check if the object has not been already disposed. In the newest visual studio there is nice code snippet that provides some sample implementation:) Anyway good job!</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
