PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Where's all my Memory Going??

I'm currently testing my game which will (hopefully) shortly be released on the Smartphone platform. While I have been testing it I have been trying to reduce the memory overhead it causes.

At the moment my game appears to use about 2.5Mb of memory. (I have yet to work out how to measure the memory overhead accurately - any advice welcome.)
This is not an unreasonable amount but it seems a little bit more than many commercial games already out on smartphone.

My first port of call was obviously this forum and I have read that changing the colour depth of PNG images can help. So I did this, changing every single image in my game from the standard 16M/24bit format that PaintShopPro saves them to, to 256Col/8bit.

The file sizes generally reduced (except for really small ones where the palette info obviously has taken up a large percentage of the file size) but the memory overhead seemed to remain absolutely the same.

I have to say I can't quite work out where this 2.5Mb has come from. I have about 70 CGapiSurfaces which are defined as global variables and are on the Stack (is this sensible?). Any surfaces which will not be used in the game again - such as intro splash screens etc - are freed by calling CGapiSurface(0,(DWORD)0,0).

The largest structure I have is a LEVEL struct. Sizeof reports that this is about 50000 bytes - so that seems ok. This is created on the heap using the new operator.

Apart from a few global DWORDs etc the only other major thing I have is a lookup table for sine and cosine values - so thats 2 arrays of doubles (should I be using doubles?) of size 360.

So thats about it. I don't profess in any way to be an expert C++ programmer so I could be making many elementary mistakes - hence all my little questions in brackets!!

If anybody has any general advice for me I would be eternally grateful. I realize what I've written is not particularly detailed but you guys all seem to be pretty clever. (My Mum always used to say that flattery will get you nowhere/everywhere!!)

One more question - in the GapiDraw docs it says a CGapiSurface takes up 64 bytes of memory. Why does sizeof(CGapiSurface) report that it's size is 300?

Anyway, thanks in advance for any help.
I believe (but may be wrong) that ALL surfaces that are create under GapiDraw are either 16 bit without alpha or 24 with alpha

this is for performance reasons, so that blits dont have to lookup every pixel's real colour in a palette before doing the actual blit

but what this also means is that despite reducing all your graphics down to 8 bit, they are stored on 16 bit surfaces in the game

if you are including the graphics in the exe, then you are also increasing the memory that the game is using

I would also suggest loading and unloading the surfaces as you need them, only holding in memory the ones you are actually needing at the time
You can find some info on how to measure the memory being used by your app in the ppcstudios GapiDraw forum (www.ppcstudios.com).

As muff indicated, all surfaces are stored as 16 bit (not sure about the alpha surfaces).

I'd suggest taking the 'simple' demo app and stripping out everything until you have a basic skeleton application which only fills the backbuffer. This will give you a baseline from which to gauge the memory usage.
One more question - in the GapiDraw docs it says a CGapiSurface takes up 64 bytes of memory. Why does sizeof(CGapiSurface) report that it's size is 300?


Thanks, I forgot to update this in the docs...
Don't know if this is of help, some talk of memory requirements at graphic load time:

http://www.pocketmatrix.com/forums/viewtopic.php?t=13025
Many thanks for all your replies.

I'm going to use the memory resource monitor function as described on the ppcstudios forum. It looks very helpful and I should be able to work out the answer to my original post title.
OK - I've been having fun with the memory logs that I've been creating. I now create some of my larger surfaces only when they are required and the memory usage is now lower when they are not being used. So I'm getting somewhere. :)

I have one further question though. When I call the function to output the memory usage to file in the InstanceMethod, the amount of free memory decreases on successive runs of my program. It's not exactly the same amount every time but tends to vary between 20k-40k. Does this mean I have a memory leak? Obviously I can't tell what my phone is doing in the background but it does seem strange that by simply executing the program, exiting, and executing it again in exactly the same way that I should lose this memory.

From what I understand (which could well be wrong) anything created with the new keyword must be deleted using the delete keyword. I also assume that any global variables just remain in memory for the duration of the program and any local variables only last for the function in which it is defined.

Are there any other general pointers anyone could give me with regards to memory mangagement. I come from a Java background where all this stuff is done for you and I realise that memory management is quite a large area of C++ programming, so any general help would be great.

Thanks again for your help so far!!
gevans2000:

One more question - in the GapiDraw docs it says a CGapiSurface takes up 64 bytes of memory. Why does sizeof(CGapiSurface) report that it's size is 300?


I thought 300 bytes sounded extremely huge, and when I call sizeof(CGapiSurface) I get 80 bytes which is the exact value. Try adding the size of the member variables together and 80 is it...
Johan,

Yes you're right - I don't know where the 300 bytes was coming from. It's possible I was doing something very stupid. My app now correctly reports the size as 80 bytes.