Garbage detector from my Gamefest Slides

One of the last things in my slides for Gamefest was a simple little object I had written to help us detect when our code was allocating memory we weren’t expecting. Due to the size limitations of slides I had attempted to to shorten the object to the minimalist version, and in doing so lost a few things. Here is the object I used in its entirety:

Not very much different than what’s in the slides, but a few subtle differences. The most important being whether or not ‘true’ or “false” is passed in during the two calls to GetTotalMemory. What does this code actually do though?

I wrote it basically to allow myself to “wrap” a section of code and measure any allocations from it. At the constructor of the helper object I measure the total memory in use and store how much allocation (in bytes) I expect the code block to take. Then at the end I measure the memory once more and if more memory was allocated than I was expecting I break into the debugger.

A few things to note. First, when you pass in ‘true’ to GetTotalMemory a full collection will occur, so if you are doing this often you will dramatically slow down your application.

Second, you’ll notice that the slides pass in ‘true’ in both spots which is probably not the behavior you want (you’ll notice in the code above that ‘false’ is passed at the end in the second constructor). If you pass in true in both spots, you will only catch allocations that were not garbage because any garbage would have been collected due to the ‘true’ being passed in!

Now, there are times when you do want to know this, so having the option is certainly fine, but the majority of time you want to know about allocations that create garbage, in which case you need to pass in false to the second call.

Another point I brought up during the talk that isn’t obvious is that this code completely ignores other threads. Which means if you have another thread allocating memory in between the two calls, you will get “false positives” that can prove difficult to track down. When I used this method I kept the code I wrapped in this object small to help eliminate them.

To see an example of using this helper in code see below:

I love books!

I suppose that isn’t a huge surprise given that I’ve written three of them and as I’ve said before writing a book can be hard work. It goes beyond that though, I’ve always been a fan of books. I used to sit up and read constantly, and while I rarely have time to read as much now as I used to, I still enjoy a good book when I have the time.

I was always a fan of fiction, although interesting non-fiction stories are pretty cool I suppose. I just liked the idea of being able to be transported to an author’s world when reading. When I was younger I used to imagine myself publishing books and being a famous author and bringing other people into world’s I’ve created. I suppose you can see the parallel’s between that and video game development as well.

The thing is, the major reason I wrote my first book on Managed DirectX was because I wanted to be an “author”. I wasn’t actively looking to write a book at the time I was approached at the Game Developers Conference back in 2003, I just happened to have a lucky series of circumstances. I had the bulk of knowledge about a subject people were interested in, I was easy to find, and I happened to be able to write in a way that people found helpful. So when I was asked to write what turned out to be the Kickstart book, I jumped at the opportunity.

I was so focused on being an “author” that I almost completely lost sight of some of the benefits. I was offered multiple contracts (from various publishers) for that first book, and I came extremely close to signing and accepting the first one I saw. All I cared about was being able to walk into a book store finding my book and saying “I wrote this!” While my first book would have been just as awesome had I actually signed that first contract, I would have missed out on quite a bit of the extra benefits. For example, both contracts had an “advance” in them (which by the way to show you my naivete surprised me). However, the ‘advance’ in the first contract gave me a LCD monitor, while the advance in the one I eventually signed gave me actual money and substantially more than the cost of a monitor. The first one also had portions of royalties paid in Amazon gift cards, and while I love Amazon, they weren’t that great back then, and even now, money is still better.

I never did it for the money though, and while I certainly won’t complain about it, it wasn’t a motivating factor. That isn’t to say I’m not glad I picked the contract I did because I certainly am. However in my second book I had lost that motivation of “becoming an author” that I had in my first book and it suffered for it. The second book is obviously inferior to the first, and it took me forever to write it because my motivation wasn’t there. It took another five years before I decided to write another book and even then I only did it once I had a co-author to help me. While I didn’t have quite the same motivation for the most recent book as the first, it’s very close in quality and information. It wasn’t “work” like the second one was (and I did like that I was helping Dean become an “author” as well).

However my initial love of books and desire to write some fiction that will enable me to bring someone into a world of my creation still exists and has grown dramatically over the last few years. I still love video games and making video games, but they are often a collaborative effort. It’s very rare that a game is made under a single vision with no deviation, while that is the norm for books.

Barring unforeseen circumstances, later this year I will be publishing my first fiction book, and I’m quite excited about it. I’m terrible at picking names so it is currently still “Untitled”, but I’m sure I’ll come up with a better title soon enough. Some of my favorite authors are Stephen King, John Saul and Dean Koontz if that might help dictate where the genre might be heading as well!