The Little Things
.Net Memory Profiler (not free, has non-crippled trial period) is a nifty little tool that lets you see where the memory usage for your .Net app is coming from. I of course used it on Bunnies because it was consuming well over 300MB of memory. Turns out that the Point Class was responsible for about 224MB of that.
The point class is used by every pixel of every texture for lighting calculations. It’s the bump map and depth map values for every pixel of the texture. According to the profiler each instance of the point class consumed 48 bytes of memory and over 4.5 million of them were in memory.
The point class consisted of 5 doubles and some methods to do basic vector math. 5 doubles is 40 bytes and the additional 8 bytes I assume keep track of where the methods are in memory. In Perl land “self” is passed into class methods which is good to know. It doesn’t matter how many methods your class has, all the methods are in memory once and self is implicitly passed in. So the methods consume memory only once and only the instance variables consume memory for each instance in memory.
Anyway, now you know how that works. Since the bump and depth maps are dealing with approximating light and high precision values are not needed, I switched the five doubles to five floats. That cut down the size of each instance to 28 bytes. 4.5 million 48 byte objects in memory results in 205MB of memory usage. 4.5 million 28 byte objects in memory results in 120MB of memory usage. 58% of the original size.
To further illustrate the absurdity of this, consider that it only takes 3 bytes to represent the RGB values of each pixel of the texture. 4.5 million RGB values take up only 13MB of memory. So the vast majority of the memory usage for Bunnies comes from lighting information.
The added bonus of switching to floats (which didn’t cause any noticable decrease in lighting effects quality) is that the game loads faster and I got about a 5fps speed boost. Up to 42fps from 37. A 13.5% increase.
The new Bunnies server will be running this weekend and the new client should be available for download then as well. I’ll be tweaking the position update frequency and playing around with other server things before I commit to a release.
Leave a comment
You must be logged in to post a comment.