{"id":726,"date":"2013-07-25T21:09:55","date_gmt":"2013-07-25T21:09:55","guid":{"rendered":"http:\/\/www.qcfdesign.com\/?p=726"},"modified":"2013-07-25T21:12:41","modified_gmt":"2013-07-25T21:12:41","slug":"on-the-drudgery-of-detective-work","status":"publish","type":"post","link":"http:\/\/www.qcfdesign.com\/?p=726","title":{"rendered":"On The Drudgery Of Detective Work"},"content":{"rendered":"<p>The graph below is something that I&#8217;ve become incredibly familiar with over the last week. That&#8217;s not to say that I wasn&#8217;t familiar with Unity&#8217;s profiler before, anyone with a reasonable eye on performance will use it every once in a while as a game matures, but this past week has been one of <em>those<\/em>. The reason is visible in the graph. A memory leak. Small, but definitely there:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.qcfdesign.com\/wp-content\/uploads\/2013\/07\/profiler2.png\" rel=\"shadowbox[sbpost-726];player=img;\"><img loading=\"lazy\" class=\"aligncenter  wp-image-727\" alt=\"Keep looking! Ignore the crazy!\" src=\"http:\/\/www.qcfdesign.com\/wp-content\/uploads\/2013\/07\/profiler2.png\" width=\"564\" height=\"254\" srcset=\"http:\/\/www.qcfdesign.com\/wp-content\/uploads\/2013\/07\/profiler2.png 806w, http:\/\/www.qcfdesign.com\/wp-content\/uploads\/2013\/07\/profiler2-300x135.png 300w\" sizes=\"(max-width: 564px) 100vw, 564px\" \/><\/a><\/p>\n<p>Did you find it? Here&#8217;s a clue: It starts off as the lowest line and ends up not being the lowest one&#8230; Yup. That strange burnt ochre that shows how many materials there are currently. The key here is that this is (pretty much) normal gameplay (apart from me spamming a specific game event) and the number of actual game objects stays relatively constant throughout. That big spike in the blue texture memory line? That&#8217;s what happens when you cast a fireball.<!--more--><\/p>\n<p>During the game, you&#8217;d expect the material count to climb, but only as much as there were new objects on the screen. Unfortunately they keep slowly, inexorably, climbing every time <em>something<\/em> happens. That something is a new particle effect being created. Figuring that out was pretty easy &#8211; as was figuring out where the material was being leaked: Unity will clone a material every time you reference a GameObject&#8217;s <strong>renderer.material<\/strong> property. It assumes that you intend to change this particular material in some way, so in order to not change every object of that type, it gives that specific instance a unique material. The problem comes in when Unity prevents materials being handled by standard garbage collection.<\/p>\n<p>Generally, this pessimistic approach is fine. We&#8217;d been bitten by this sort of reference-loss problem before, but for some reason in the case of particle systems, an extra clone of an otherwise brand-spanking new material was being created, which we weren&#8217;t holding on to and thus was being leaked into the ether of graphics memory where it would be useless to everyone. Eventually, at some hideous time, I stumbled on the right order of operations to prevent this superfluous material&#8217;s creation. So now that burnt ochre bloody-well stays put.<\/p>\n<p>Also, the game should be slightly faster and not do that thing where it crashes after you run it nonstop for 3 years&#8230; It was a really small memory leak and modern PCs have a lot of room.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The graph below is something that I&#8217;ve become incredibly familiar with over the last week. That&#8217;s not to say that I wasn&#8217;t familiar with Unity&#8217;s profiler before, anyone with a reasonable eye on performance will use it every once in a while as a game matures, but this past week has been one of those. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[19,18],"_links":{"self":[{"href":"http:\/\/www.qcfdesign.com\/index.php?rest_route=\/wp\/v2\/posts\/726"}],"collection":[{"href":"http:\/\/www.qcfdesign.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qcfdesign.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qcfdesign.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qcfdesign.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=726"}],"version-history":[{"count":0,"href":"http:\/\/www.qcfdesign.com\/index.php?rest_route=\/wp\/v2\/posts\/726\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.qcfdesign.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qcfdesign.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qcfdesign.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}