strange(?) issue pertaining to memory usage

BlitzMax Forums/BlitzMax Programming/strange(?) issue pertaining to memory usage

Bukky(Posted 2006) [#1]
Hello again,

Sorry about all of these threads, but its crunch time over here and we're trying to get a lot done all at once.

ANYHOW....

I've been working on optimizing my code in oder to reduce memory usage (changed whatever images I could to power of two, dynamically loading music as needed), but my game still takes up massive amounts of memory.

I've been monitoring memory usage via Windows Task Manager in Windows XP, and this is what happens when I run my game:

Game loaded up, user is at menu screen (modest amount of RAM)

User goes to first stage (RAM shoots up as expected)

User returns to menu screen (RAM shoots up AGAIN(wth??))

User goes to level two (RAM shoots up)

User goes to level three (RAM shoots up)

User loses and goes back to menu (RAM stays constant)

User goes back to level one (RAM stays constant)


By the time all is said and done, Windows Task Manager is registering well over 200mb of RAM for my process. What in the world is going on here? All of my images are loaded at runtime, and it seems that the memory goes up substantially when those images are in use. Now, why doesnt it go back down when those particular images are not being drawn? It seems that there is a compounding of memory somewhere.

Is there something obvious I'm not doing? Anyone have suggestion?


Chris C(Posted 2006) [#2]
the garbage collector uses a pool of memory because allocating memory via the OS is expensive in terms of time.

you need to set all the resources (and *ALL* ) references to level two and three to null before the GC will release them

freed memory is kept in the memory pool, but if unused is usually released after a while

You'll have to check carefully you haven't got some reference to somthing refering to an object in level 2 & 3


Bukky(Posted 2006) [#3]
Hey Chris,

Thanks for the reply.

So you think creating a function which unloads all of my variables between loading stages to null will solve at least some of my RAM woes?


FlameDuck(Posted 2006) [#4]
Maybe. It depends on your data structure. In an ideal world, emptying your collections should clear any references you have. If oit doesn't, see if you managed to make an cyclic data structures that are being orphaned.


Chris C(Posted 2006) [#5]
as flameduck says it depends on your data structure, chasing down a cyclic data structure problem could be a nightmare

but in theory nulling all the stuff thats used should work

another idea to consider is have a function to "play" your level that uses only local vars, when the function returns from playing the level they will all go out of scope and be released

you could make it generic playlevel(l) and load in whatever data you need for level l, you could even get flash and use a function pointer for special code pertaining to specific levels...

the key is to use local vars in a function wherever possible


Bukky(Posted 2006) [#6]
Hey guys,

I just ended up making some routines which dynamically load and unload images as needed. This, combined with changing my images to power of 2 has reduced my RAM consumption by half!

Are there any build options that would help me further reduce it though? Anything else that might be obvious to Blitz veterans but overlooked by Blitz newcomers such as myself?


Dreamora(Posted 2006) [#7]
No there aren't any settings that reduce the RAM usage.
The GC is already quite optimized in low ram usage
(.NET GC for example would not free anything automatically as long as there is more ram to allocate as it takes time. This shows that it theoretically is not needed to free unless there is a shortage in memory)


Chris C(Posted 2006) [#8]
I'd have a look at framework assistant, that may well bring down the size of the exe and under some circumstances the mem usage too (if you're very lucky!)


Bukky(Posted 2006) [#9]
Thanks!