Tuesday, August 3, 2010

Memory Conservation

I love memory.
I like the way it sits there waiting to be used for any purpose you would like.
I like the way it never judges you.
I like the way it can be wiped once you have used it.
I like the way everyone wastes too much of it.
I like the fact i am good at finding more of it.

In a nutshell, the memory in your computer, or the memory in your console is of a finite amount. It can only hold so much. However it is amazing just how much more it can hold than you ever believed possible. Now don't get me wrong, nowadays most people don't care too much about memory. The basic, 'Does it fit' question is answered, and if it is in the negative, then stuff must be removed. However my personal favourite thing to do is just find more. This can be done in many ways, but here is a simple example to start with.

boolean, this has a True or False value. so why is it generally now a long? (longs take 4 bytes!). The main reason of course is performance. It is just much easier to cache along multi-byte boundaries. Address space registers can be smaller if they do not have to visit every byte. But this simple value takes up a whole whopping 4 bytes. That is 32X the amount it needs to work. So one of those items that can save memory is placing all your boolean values together and then just simply bit testing for your value. That was you get an amazing 32X compression.

Another way to fit more in is simply to compress it, making your textures half sized in the width and height will yield great savings, but of course you are sacrificing the look of your textures. OR are you? I ask this because it has been my experience that the textures programmers tend to be given are way too big for their purpose. If for example you were given a 1024x1024 texture and it was mapped to something that you could only get half distance to. so it filled about half the screen, then you are double your texel overdraw, so making the renderer draw something not at it's perfect size and letting the renderer make the decision about how to chop it down. In this case halving the texture would save memory and noone would notice the difference, or if they did, they would notice it looked better.

Yesterday and today i have been saving memory in Darlas Den and The Midway for PS3 Home. In this case i am finding a lot of memory simply because i have been given a new command. The power to set a texture (or replace an existing one actually). This power cannot be underestimated as to how much i can achieve. I have already saved something like 10% of the memory used in Darlas Den. Yes it is a bit of work, but this will allow us to put more in there than even we suspected i am sure.

I do enjoy saving memory, it is something that i appear to be quite skilled at, even when we feel the code and assets are as tight as can be, i can go in and shave a few more K out of it.

This last note is to mention the fact that at Mass Media we have done and are doing  a number of conversions. Usually to much lower spec machines. I am sure a lot of my skill comes from this. However it still amazes me how companies can give us a game to convert that does not even fit on their main machine until after some cost cutting of their own. Still i guess it is a lower priority than actually getting it finshed. (as it should be!)



  1. This is why game devs need to understand computer systems properly. Know people wanting to be developers who will never begin to understand how to look after their memory usage.

  2. when i started writing this piece i realised that it is a HUGE area for discussion. So i stayed to some very basic writing, even though i really wanted to go into memory allocators and how they work and how they waste memory etc.

    Talking about the cost of of allocating and especially re-allocating would be a good idea too. Maybe another post is gonna be required.

    Mostly nowadays programmers are separated from having to consider memory, unltil you run out. Any managed language is a great example of that.

    So yeah, i think it would be an invaluable lesson for game devs to understand this kind of thing if they don't already.

    Da Voodoochief

  3. I agree. Finding memory is so much fun. It's amazing how much can be found with just a little work. From working on handhelds and integrated systems to consoles, I still always feel a little amazed at just how far memory can go. And like you said, it's not about reducing quality but about realizing how much slop is involved even in the simple things. Alignment is huge, it's not just bools but larger data types too. I can't tell you how many times I've found a byte variable followed by a 16 byte aligned matrix.

  4. oh yeah, i forgot about those classes with more space in them than actual data.

    Da Voodoochief