<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL Server Compact - Compact &amp; Capable : GC.WaitForPendingFinalizers</title><link>http://blogs.msdn.com/sqlservercompact/archive/tags/GC.WaitForPendingFinalizers/default.aspx</link><description>Tags: GC.WaitForPendingFinalizers</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>SQL Server Compact Garbage Collection – Whys and Hows</title><link>http://blogs.msdn.com/sqlservercompact/archive/2009/05/05/sql-server-compact-garbage-collection-whys-and-hows.aspx</link><pubDate>Tue, 05 May 2009 17:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9588985</guid><dc:creator>SQLCEBLOG</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/sqlservercompact/comments/9588985.aspx</comments><wfw:commentRss>http://blogs.msdn.com/sqlservercompact/commentrss.aspx?PostID=9588985</wfw:commentRss><description>&lt;H4&gt;&lt;B&gt;&lt;U&gt;&lt;FONT color=#0080c0&gt;Some background about SQL Server Compact:&lt;/FONT&gt;&lt;/U&gt;&lt;/B&gt; &lt;/H4&gt;
&lt;P&gt;&lt;B&gt;&lt;U&gt;&lt;/U&gt;&lt;/B&gt;
&lt;P&gt;SQL Server Compact is an embedded database implemented in native and it can be accessed in application by either OLEDB or ADO.NET provider model. 
&lt;P&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.aspx"&gt;&lt;FONT color=#0066a7&gt;ADO.NET provider for SQL Server Compact&lt;/FONT&gt;&lt;/A&gt; is a managed assembly which depends on native SQL Server Compact DLLs to provide the service. In general, the managed classes under ADO.NET provider model for SQL Server Compact are just wrappers around the native classes. 
&lt;P&gt;SQL Server Compact native objects are dependent. For instance, Cursors and Transactions are dependent on Connection object and are expected to be released in the right order. 
&lt;P&gt;&lt;B&gt;&lt;FONT color=#0080c0&gt;What’s the problem then?&lt;/FONT&gt;&lt;/B&gt; In native application, the developer can dispose the cursors, commands, etc. before closing the connection but in managed application with &lt;A href="http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx" mce_href="http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx"&gt;&lt;FONT color=#0066a7&gt;.Net Garbage Collector&lt;/FONT&gt;&lt;/A&gt; there is no particular order in which these objects will be disposed. 
&lt;P&gt;&lt;B&gt;&lt;FONT color=#0080c0&gt;So what do we do?&lt;/FONT&gt;&lt;/B&gt; Our &lt;B&gt;SqlCeConnection&lt;/B&gt; object maintains &lt;A href="http://msdn.microsoft.com/en-us/library/ms404247.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms404247.aspx"&gt;&lt;FONT color=#0066a7&gt;weak references&lt;/FONT&gt;&lt;/A&gt; to all objects tied with it. Or to be specific, it maintains short weak references. 
&lt;P&gt;&lt;B&gt;&lt;FONT color=#0080c0&gt;How does it help me in disposing the objects in order?&lt;/FONT&gt;&lt;/B&gt; When the connection object is getting disposed, the weak reference cache is iterated and all the objects there are disposed before the connection object getting disposed. Please note that when we say Dispose, we also mean Finalization. 
&lt;H4&gt;&lt;B&gt;&lt;U&gt;&lt;FONT color=#0080c0&gt;How does .Net Garbage Collector comes into picture of SQL Server Compact&lt;/FONT&gt;&lt;/U&gt;&lt;/B&gt; &lt;/H4&gt;
&lt;P&gt;Simply employing short weak references to keep track of all related objects and dispose them in sequence does not solve all the problems. 
&lt;P&gt;&lt;B&gt;&lt;FONT color=#0080c0&gt;What is the problem then?&lt;/FONT&gt;&lt;/B&gt; Sometimes the database files can remain locked in stress scenarios because not all the dependent objects are disposed upon dispose of SqlCeConnection. This happens because we use short weak refs to track object lifetime and we could end up in situations in which an object is in the finalization queue (hence the short reference is not longer alive) and isn’t in the freachable queue (hence isn’t guaranteed to be picked up by the GC immediately). More information about short weak references can be found &lt;A href="http://msdn.microsoft.com/en-us/library/ms404247.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms404247.aspx"&gt;&lt;FONT color=#0066a7&gt;here&lt;/FONT&gt;&lt;/A&gt;. 
&lt;P&gt;All this leads to a situation, where the database file is still locked even though customer application closed the connection.&amp;nbsp; Hence, it is not able to get deleted the file using file explorer …etc means. 
&lt;P&gt;&lt;B&gt;&lt;FONT color=#0080c0&gt;How did we solve the problem?&lt;/FONT&gt;&lt;/B&gt; When we are cleaning weak reference cache in dispose of&amp;nbsp; SqlCeConnection object, we need to call &lt;B&gt;GC.WaitForPendingFinalizers()&lt;/B&gt;&amp;nbsp; before we return to the caller; in this way, we are guranteed that the&amp;nbsp; GC will pick up all finalizable objects and hence all the native&amp;nbsp; interfaces will be properly released even if we no longer have short&amp;nbsp; references to them. 
&lt;P&gt;More information about this API can be found &lt;A href="http://msdn.microsoft.com/en-us/library/system.gc.waitforpendingfinalizers.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.gc.waitforpendingfinalizers.aspx"&gt;&lt;FONT color=#0066a7&gt;here&lt;/FONT&gt;&lt;/A&gt;. 
&lt;H4&gt;&lt;FONT color=#0080c0&gt;&lt;B&gt;&lt;U&gt;FAQs&lt;/U&gt;&lt;/B&gt; &lt;/FONT&gt;&lt;/H4&gt;
&lt;P&gt;&lt;FONT color=#0080c0&gt;&lt;B&gt;1. &lt;/B&gt;&lt;B&gt;Why are we employing short weak references and not long weak references?&lt;/B&gt; &lt;/FONT&gt;
&lt;P&gt;&lt;B&gt;&lt;FONT color=#0080c0&gt;Ans.:&lt;/FONT&gt;&lt;/B&gt; SQL Server Compact ships for both Desktops as well as Devices. Long weak references are not supported in .Net CF. More information &lt;A href="http://msdn.microsoft.com/en-us/library/sk6k8h17.aspx" mce_href="http://msdn.microsoft.com/en-us/library/sk6k8h17.aspx"&gt;&lt;FONT color=#0066a7&gt;here&lt;/FONT&gt;&lt;/A&gt;. 
&lt;P&gt;&lt;B&gt;&lt;/B&gt;
&lt;P&gt;&lt;FONT color=#0080c0&gt;&lt;B&gt;2. &lt;/B&gt;&lt;B&gt;Does call to GC.WaitForPendingFinalizers() has negative performance impact?&lt;/B&gt; &lt;/FONT&gt;
&lt;P&gt;&lt;B&gt;&lt;FONT color=#0080c0&gt;Ans.:&lt;/FONT&gt;&lt;/B&gt; No. Most of the time, the freachable queue&amp;nbsp; is empty (the same is not true for finalization queue though). See &lt;A href="http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx" mce_href="http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx"&gt;&lt;FONT color=#0066a7&gt;here&lt;/FONT&gt;&lt;/A&gt; for more information about Garbage Collector. 
&lt;P&gt;&lt;FONT color=#0080c0&gt;&lt;B&gt;3. &lt;/B&gt;&lt;B&gt;Can this call to GC.WaitForPendingFinalizers() lead to deadlocks?&lt;/B&gt; &lt;/FONT&gt;
&lt;P&gt;&lt;B&gt;&lt;FONT color=#0080c0&gt;Ans.:&lt;/FONT&gt;&lt;/B&gt; Yes. If Dispose method of SQL CE objects are called in the finalization context than the explicit dispose context. The reason being when an object is getting &lt;B&gt;&lt;I&gt;finalized&lt;/I&gt;&lt;/B&gt;, it is not supposed to touch any other managed objects as the currently getting disposed object does not know the life-status of the object it is trying to refer. 
&lt;P&gt;&lt;FONT color=#0080c0&gt;&lt;B&gt;4. &lt;/B&gt;&lt;B&gt;Why did we choose this design?&lt;/B&gt; &lt;/FONT&gt;
&lt;P&gt;&lt;B&gt;&lt;FONT color=#0080c0&gt;Ans.:&lt;/FONT&gt;&lt;/B&gt; SQL CE was primarily designed for Devices where the memory, processing comes at very high cost.&amp;nbsp; So, it has been designed to free up the resources as early as we can and hence it is calling GC.WaitForPendingFinalizers. 
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;FONT face=Calibri&gt;Thanks,&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"&gt;&lt;FONT face=Calibri&gt;Mohammad Imran.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9588985" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/sqlservercompact/archive/tags/SQL+CE+Garbage+Collection/default.aspx">SQL CE Garbage Collection</category><category domain="http://blogs.msdn.com/sqlservercompact/archive/tags/GC.WaitForPendingFinalizers/default.aspx">GC.WaitForPendingFinalizers</category></item></channel></rss>