{"id":365,"date":"2010-05-21T23:30:13","date_gmt":"2010-05-21T22:30:13","guid":{"rendered":"http:\/\/www.frozax.com\/blog\/?p=365"},"modified":"2015-04-04T21:49:03","modified_gmt":"2015-04-04T21:49:03","slug":"tips-avoid-memory-allocations-xnac","status":"publish","type":"post","link":"https:\/\/www.frozax.com\/blog\/2010\/05\/tips-avoid-memory-allocations-xnac\/","title":{"rendered":"3 Simple Tips to Avoid Memory Allocations with XNA\/C#"},"content":{"rendered":"<p>Long time without posting. I&#8217;ve spent a lot of time working on the game, contracting art, buying sound effect and music assets, designing the levels&#8230; New screenshots\/video should come very soon.<\/p>\n<p><em>Anyway, back on topic<\/em>: when developing from C++ to C#, I found out very strange that you can allocate memory but you cannot free it and have to let the garbage collector do it. But I finally got quickly used to it and found it quite handy&#8230; Until I found out that garbage collection is not free and usually causes framerate drops, especially on X360.<\/p>\n<p><a href=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2010\/05\/clrprof.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-372\" title=\"clrprof\" src=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2010\/05\/clrprof-300x132.jpg\" alt=\"clrprof\" width=\"300\" height=\"132\" \/><\/a><\/p>\n<p>Using the <a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyId=A362781C-3870-43BE-8926-862B40AA0CD0&amp;displaylang=en\">CLR Profiler<\/a> (screenshot above), you can easily see where you are wasting memory (well, it can be scary the first time you see it but in fact, it&#8217;s easy). I nearly removed all my runtime allocations with this tool, here are the few tips that can be useful:<\/p>\n<p><em><strong>1. Be careful with the class string<\/strong><\/em><\/p>\n<p>String is allocating memory every time it returns a new string. <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.text.stringbuilder%28VS.71%29.aspx\">StringBuilder<\/a> is the key to the problem. I didn&#8217;t even know what StringBuilder was before working on memory and now I use it everywhere I need to take care about memory. There are some awesome tips (and source code) about StringBuilder on <a href=\"http:\/\/www.musictomakegamesby.com\/\">Gavin Pugh&#8217; blog<\/a>. Especially <a href=\"http:\/\/www.musictomakegamesby.com\/2010\/04\/05\/xnac-a-garbage-free-stringbuilder-format-method\/\">this<\/a> and <a href=\"http:\/\/www.musictomakegamesby.com\/2010\/03\/23\/xnac-stringbuilder-to-string-with-no-garbage\/comment-page-1\/\">this<\/a>. You have everything you need now \ud83d\ude42<\/p>\n<p><em><strong>2. Reuse your lists<\/strong><\/em><\/p>\n<p>I had multiple sections of code creating a temporary List&lt;&gt; in order to add elements to need a special treatment. This was in an update function in my case. It is handy and fast to write code like that, but it allocates memory. To fix this, I know have the List&lt;&gt; as a member of the class. I <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dwb5h52a.aspx\">clear<\/a> it and reuse it when needed. When creating a list, you should use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dw8e0z9z.aspx\">constructor with an int<\/a> used to define the default capacity of the list. (List&lt;Vector2&gt; l = new List&lt;Vector2&gt;(64) for instance).<\/p>\n<p><em><strong>3. foreach loops<\/strong><\/em><\/p>\n<p>I converted my foreach loops in C++ like for loops and it saved me some allocations. Memory allocated due to foreach loops on lists is showing up in the CLR Profiler as <em>System.Collections.Generic.List&lt;T&gt;<\/em>.<\/p>\n<p><em><strong>Conclusion<\/strong><\/em><\/p>\n<p>I hope you&#8217;ll find this useful. In Spring Up Harmony, I still have some run-time allocations, mostly in the particle engine, when a new fx is launched (I use <a href=\"http:\/\/mpe.codeplex.com\/\">Mercury<\/a> for now). However, it seems that the garbage collector is not causing hick-ups therefore I&#8217;m ok with it.<\/p>\n<div class=\"addtoany_share_save_container addtoany_content_bottom\"><div class=\"a2a_kit a2a_kit_size_32 addtoany_list a2a_target\" id=\"wpa2a_1\"><a class=\"a2a_button_twitter\" href=\"http:\/\/www.addtoany.com\/add_to\/twitter?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F05%2Ftips-avoid-memory-allocations-xnac%2F&amp;linkname=3%20Simple%20Tips%20to%20Avoid%20Memory%20Allocations%20with%20XNA%2FC%23\" title=\"Twitter\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_facebook\" href=\"http:\/\/www.addtoany.com\/add_to\/facebook?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F05%2Ftips-avoid-memory-allocations-xnac%2F&amp;linkname=3%20Simple%20Tips%20to%20Avoid%20Memory%20Allocations%20with%20XNA%2FC%23\" title=\"Facebook\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_google_plus\" href=\"http:\/\/www.addtoany.com\/add_to\/google_plus?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F05%2Ftips-avoid-memory-allocations-xnac%2F&amp;linkname=3%20Simple%20Tips%20to%20Avoid%20Memory%20Allocations%20with%20XNA%2FC%23\" title=\"Google+\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_reddit\" href=\"http:\/\/www.addtoany.com\/add_to\/reddit?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F05%2Ftips-avoid-memory-allocations-xnac%2F&amp;linkname=3%20Simple%20Tips%20to%20Avoid%20Memory%20Allocations%20with%20XNA%2FC%23\" title=\"Reddit\" rel=\"nofollow\" target=\"_blank\"><\/a>\n<script type=\"text\/javascript\"><!--\nwpa2a.script_load();\n\/\/--><\/script>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Long time without posting. I&#8217;ve spent a lot of time working on the game, contracting art, buying sound effect and music assets, designing the levels&#8230; New screenshots\/video should come very soon. Anyway, back on topic: when developing from C++ to C#, I found out very strange that you can allocate memory but you cannot free&hellip; <a class=\"more-link\" href=\"https:\/\/www.frozax.com\/blog\/2010\/05\/tips-avoid-memory-allocations-xnac\/\">Continue reading <span class=\"screen-reader-text\">3 Simple Tips to Avoid Memory Allocations with XNA\/C#<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,12,21],"tags":[94,104,113,114,141],"_links":{"self":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/365"}],"collection":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":1,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":1702,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions\/1702"}],"wp:attachment":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}