{"id":193,"date":"2006-08-20T00:08:07","date_gmt":"2006-08-20T06:08:07","guid":{"rendered":"http:\/\/jameskovacs.com\/2006\/08\/20\/Dead+References+And+The+GC"},"modified":"2006-08-20T00:08:07","modified_gmt":"2006-08-20T06:08:07","slug":"dead-references-and-the-gc","status":"publish","type":"post","link":"https:\/\/www.jameskovacs.com\/index.php\/2006\/08\/20\/dead-references-and-the-gc\/","title":{"rendered":"Dead References and the GC"},"content":{"rendered":"<p>Here&#8217;s an interesting bit of CLR trivia for you. The CLR behaves differently with respect to how aggressively it considers references dead depending on whether or not a debugger is attached. Take the following code as an example:<\/p>\n<div style=\"FONT-SIZE: 11pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Consolas, Courier, sans-serif\">\n<p style=\"MARGIN: 0px\"><span style=\"COLOR: blue\">public<\/span> <span style=\"COLOR: blue\">void<\/span> Foo() {<\/p>\n<p style=\"MARGIN: 0px\">&nbsp;&nbsp;&nbsp; <span style=\"COLOR: teal\">Bar<\/span> b = <span style=\"COLOR: blue\">new<\/span> <span style=\"COLOR: teal\">Bar<\/span>();<\/p>\n<p style=\"MARGIN: 0px\">&nbsp;&nbsp;&nbsp; <span style=\"COLOR: green\">\/\/ Do some stuff with b<\/span><\/p>\n<p style=\"MARGIN: 0px\">&nbsp;&nbsp;&nbsp; <span style=\"COLOR: blue\">for<\/span>(<span style=\"COLOR: blue\">int<\/span> i = 0; i &lt; <span style=\"COLOR: blue\">int<\/span>.MaxValue; i++) {<\/p>\n<p style=\"MARGIN: 0px\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DoSomething();<\/p>\n<p style=\"MARGIN: 0px\">&nbsp;&nbsp;&nbsp; }<\/p>\n<p style=\"MARGIN: 0px\">&nbsp;&nbsp;&nbsp; <span style=\"COLOR: green\">\/\/ What does b point to here?<\/span><\/p>\n<p style=\"MARGIN: 0px\">&nbsp;&nbsp;&nbsp; <span style=\"COLOR: green\">\/\/ What if we add GC.KeepAlive(b);<\/span><\/p>\n<p style=\"MARGIN: 0px\">}<\/p>\n<\/div>\n<p>Note that the local variable, <em><strong>b<\/strong><\/em>, is not referenced in the loop or afterwards. If we are debugging the application and examine the local variable, <strong><em>b<\/em><\/strong>, after the loop, <strong><em>b<\/em><\/strong> still points to the new Bar instance created at the top of the method because&nbsp;<strong><em>b<\/em><\/strong> is still accessible in the current scope. If a debugger is not attached, the GC notes that <strong><em>b<\/em><\/strong> isn&rsquo;t used in the loop or afterward and collects it aggressively. The reason for the difference in behaviour is for ease of debugging.<\/p>\n<p>Another interesting note is the effect of GC.KeepAlive. By adding a call to GC.KeepAlive to the end of the Foo method, we are extending the lifetime of b, regardless of whether a debugger is attached or not, because we are passing the <strong><em>b<\/em><\/strong> reference to a method. The method body of GC.KeepAlive doesn&#8217;t contain any code. Its sole purpose is to keep the reference alive so that it is not collected by the GC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an interesting bit of CLR trivia for you. The CLR behaves differently with respect to how aggressively it considers references dead depending on whether or not a debugger is attached. Take the following code as an example: public void Foo() { &nbsp;&nbsp;&nbsp; Bar b = new Bar(); &nbsp;&nbsp;&nbsp; \/\/ Do some stuff with b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-193","post","type-post","status-publish","format-standard","hentry","category-dotnetgeneral"],"_links":{"self":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/comments?post=193"}],"version-history":[{"count":0,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jameskovacs.com\/index.php\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}