{"id":23176,"date":"2006-08-18T19:14:00","date_gmt":"2006-08-18T19:14:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/maoni\/2006\/08\/18\/i-am-a-happy-janitor-part-1-finding-garbage\/"},"modified":"2021-10-04T16:34:35","modified_gmt":"2021-10-04T23:34:35","slug":"i-am-a-happy-janitor-part-1-finding-garbage","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/i-am-a-happy-janitor-part-1-finding-garbage\/","title":{"rendered":"I Am a Happy Janitor \u2013 Part 1: Finding garbage"},"content":{"rendered":"<p><span>Indeed what I do is very much like the job of the janitors \u2013 like the ones who clean  your building, or janitors you see at a food court, or yourself when you are taking care of garbage at your house. Doubtful you say? Let me  prove it to you.<\/span><\/p>\n<p><strong><span>Finding garbage<\/span><\/strong><\/p>\n<p><span>What do you do when you are having lunch with your friends at a food court  and suddenly you need to go somewhere for a short while in the middle of it (like, you asked for hot sauce but were given not-so-hot-sauce and  you went back to the cashier to tell him how it\u2019s so not up to your  standard)? Then the nice janitor guy comes along and he sees your plate  and tries to take it (you were so hungry you already ate most of your  food before your remembered to use the sauce\u2026). What happens now?<\/span><\/p>\n<p><span>One of your friends says to the janitor guy, \u201cmy friend is not finished  yet.\u201d. The janitor then goes away without taking your plate.<\/span><\/p>\n<p><span>Same principle for the GC, the difference is GC\u2019s friends tell it what\u2019s <\/span><strong><span>not<\/span><\/strong><span> garbage, ie, what\u2019s live (seriously..who wants garbage\u2026). GC\u2019s friends include:<\/span><\/p>\n<p><span>\u00b7     JIT<\/span><\/p>\n<p><span>\u00b7     EE stack walker<\/span><\/p>\n<p><span>\u00b7     Handle table<\/span><\/p>\n<p><span>\u00b7     Finalize queue<\/span><\/p>\n<p><span>What happens is the following:<\/span><\/p>\n<p><strong><span>GC<\/span><\/strong><span> says: I need to start a collection on gen X\u2026My super friends please report to me what is live now for gen X.<\/span><\/p>\n<p><strong><span>JIT<\/span><\/strong><span> says: Roger that. I will take care of the live roots on managed stack frames. When I jitted methods I had already built up a table that tracked which locals were alive at  which point so I will report the ones that are currently live to you. I  am quite aggressive at determining if something should be dead or not in retail builds \u2013 locals <\/span><em><span>could<\/span><\/em><span> be treated as dead as soon as I  determine it\u2019s not in use anymore (not guaranteed). However in  debuggable builds I am much more lenient \u2013 I will let all locals live  till the end of the scope to make debugging more convenient.<\/span><\/p>\n<p><strong><span>EE stack walker<\/span><\/strong><span> says: I can take care of the locals on the unmanaged frames when managed  frames call into the EE (for lock implementation and whatnot). <\/span><\/p>\n<p><strong><span>Finalize queue<\/span><\/strong><span> says: Indeed, the almighty me brought back some dead objects back to life so I am telling you about these objects.<\/span><\/p>\n<p><strong><span>Handle table<\/span><\/strong><span> says: Reporting handles in use in gen X and all its youngest generations now.<\/span><\/p>\n<p><span>GC waits patiently while the super friends are reporting what\u2019s live. <\/span><\/p>\n<p><span>The only time GC needs to participate in finding what\u2019s live is when GC  needs to find out if there are objects in older generations that are  referring to objects in younger generations. For example if GC is  collecting gen 1, it will need to go find out if there are any gen2  objects that are referring to gen1 and gen0.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Indeed what I do is very much like the job of the janitors \u2013 like the ones who clean your building, or janitors you see at a food court, or yourself when you are taking care of garbage at your house. Doubtful you say? Let me prove it to you. Finding garbage What do you [&hellip;]<\/p>\n","protected":false},"author":3542,"featured_media":58792,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685],"tags":[3010,3011],"class_list":["post-23176","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","tag-general","tag-maoniposts"],"acf":[],"blog_post_summary":"<p>Indeed what I do is very much like the job of the janitors \u2013 like the ones who clean your building, or janitors you see at a food court, or yourself when you are taking care of garbage at your house. Doubtful you say? Let me prove it to you. Finding garbage What do you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/23176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/3542"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=23176"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/23176\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=23176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=23176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=23176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}