Geeks With Blogs
Bob Taco Industries Blogging Division

Many times I’ve found myself wondering how much GC memory some operation allocates. This is primarily in the context of XNA games due to the desire to avoid generating garbage and thus triggering a GC collection. Many times I’ve written simple programs to check allocations. I did it again recently.

It occurred to me that many XNA developers find themselves asking this question from time to time. So I cleaned up my sample and published it on my website. Feel free to download it and put it to use. It’s rather thoroughly commented. The location where you insert the code you wish to check is in the Update method found in Game1.cs. The default that I put in is a line of code that generates a new Guid using Guid.NewGuid (which, if you’re curious, does not create any heap allocations).

Read all of the comments in the Update method (at the very least) to make sure that your code is measured properly. It’s important to make sure that you meaningfully reference any thing you create after the second call to get the memory or else (in Release configuration at least) you will likely get incorrect results. Anyway, it should make sense when you read the comments and if not, feel free to post a comment here or ask me on Twitter.

You can find my utilities and code samples page here: http://www.bobtacoindustries.com/developers/utils/Default.aspx

To download CheckMemoryAllocationGame’s source code directly: http://www.bobtacoindustries.com/developers/utils/CheckMemoryAllocationGame.zip

(If you’re looking to do this outside of the context of an XNA game, the measurement code in the Update method can easily be adapted into, e.g., a C# Windows Console application. In the past I mostly did that, actually. But I didn’t feel like adding references to all the XNA assemblies this time and… anyway, if you want you can easily convert it to a console application. If there’s any demand for it, I’ll do it myself and update this post when I get a chance.)

Posted on Tuesday, March 1, 2011 11:30 AM xna , C# , tutorial | Back to top


Comments on this post: CheckMemoryAllocationGame Sample

Comments are closed.
Comments have been closed on this topic.
Copyright © Michael B. McLaughlin | Powered by: GeeksWithBlogs.net