{"id":180,"date":"2010-10-12T22:26:55","date_gmt":"2010-10-12T21:26:55","guid":{"rendered":"http:\/\/www.davidaddis.com\/?p=180"},"modified":"2010-10-13T21:56:24","modified_gmt":"2010-10-13T20:56:24","slug":"xna-allocation-free-string-class","status":"publish","type":"post","link":"https:\/\/www.davidaddis.com\/?p=180","title":{"rendered":"XNA Allocation-free string class"},"content":{"rendered":"<p>Hello,<\/p>\n<p>I&#8217;m developing an XNA game for Xbox and PC, and about half-way through my development cycle I realised I had a problem with memory allocation &#8211; or more precisely, with garbage collection.<\/p>\n<p>Garbage Collection (GC) occurs when the XNA framework determines that you have allocated and freed a certain amount of memory (currently 1MB), and attempts to return the freed memory to the system so it can be re-used.  It&#8217;s a useful and elegant system that makes creating software easy and fun.  However, when it comes to performance, especially on the Xbox, it might bite you in the ass.  A single garbage collection can cause a stutter in your application, and if this is happening multiple times per second, it&#8217;s going to make your app unusable.<\/p>\n<p>So I went through my game and removed all the unnecessary allocation and freeing, and replaced it with static allocation and pools of objects to re-use.<\/p>\n<p>I also created this string class, which uses a static 256 char buffer.  It has lots of functions for formatting text and numbers, and a function for getting a plain C# string out of it.  I found it very useful&#8230;  Perhaps you will too?  Feel free to use it in your projects.<\/p>\n<p>Remember to make it a member or a static variable, don&#8217;t re-create it every frame!  And don&#8217;t create a silly number of them&#8230;  Every 4 will cost you (just over) 1KB of memory.  \ud83d\ude42<\/p>\n<p><a title=\"ESPString class\" href=\"http:\/\/davidaddis.com\/Code\/ESPString.cs\" target=\"_self\">ESPString.cs<\/a><\/p>\n<p><a href=\"http:\/\/www.esp-games.com\/\">ESP Games<\/a> is the name of my indie development studio (which is&#8230; \u00a0me).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, I&#8217;m developing an XNA game for Xbox and PC, and about half-way through my development cycle I realised I had a problem with memory allocation &#8211; or more precisely, with garbage collection. Garbage Collection (GC) occurs when the XNA framework determines that you have allocated and freed a certain amount of memory (currently 1MB), &hellip; <a href=\"https:\/\/www.davidaddis.com\/?p=180\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">XNA Allocation-free string class<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[29,30,28,27,26,25],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-programming","tag-allocation","tag-games-development","tag-garbage-collection","tag-string","tag-xbox","tag-xna"],"_links":{"self":[{"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=180"}],"version-history":[{"count":8,"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions\/188"}],"wp:attachment":[{"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.davidaddis.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}