CheckMemoryAllocationGame Sample
Posted
by Michael B. McLaughlin
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Michael B. McLaughlin
Published on Tue, 01 Mar 2011 16:30:09 GMT
Indexed on
2011/03/01
23:25 UTC
Read the original article
Hit count: 317
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.)
© Geeks with Blogs or respective owner