Remove images from memory

BlitzMax Forums/BlitzMax Programming/Remove images from memory

Ravl(Posted 2013) [#1]
I read some topics from the forums about this but I still have some issues/questions.

My game has 80 scenes, hundreds of objects, 120 additional graphics files and some animations.

I want to keep a low memory usage, so i am loading the graphics for a scene only when i am entering that scene. I have a class: TScene and only 1 instance of this class is used.

But for my additional graphics and animations i have separate objects. I observed that while playing the game my memory is going up to 600 mb so I guess that is somethign wrong.

I am using:

imageAdditionalGraphics[x] = null


to unload an image.

Is there another way to do that?


Yasha(Posted 2013) [#2]
I observed that while playing the game my memory is going up to 600 mb so I guess that is somethign wrong.


Does it stay there or does it grow infinitely if you keep unloading and reloading scenes?

Bear in mind that even if you free your resources properly, many programs (especially those with any kind of automatic memory manager like a GC) don't actually give the underlying space back to the OS unless they're forced to do so. BlitzMax could be holding onto it with the expectation that you'll be needing that memory again shortly for the next scene; no point giving it back and immediately requesting it from the system again.

So if the memory usage stays constant across several scene loads/unloads, there isn't necessarily a bug, it could just be that you're using a lot of space for that scene stuff, and you need to optimise rather than fix.


That said graphics resources normally need to be explicitly freed rather than just GC'd, don't they? (I don't know)


Ravl(Posted 2013) [#3]
I just hit 800 in a scene with a lot of animations which is bad.

If I am going back and forward between 2 simple scenes, the memory usage stays constant.

Also I have these minigames. I never "unload the images" after finishing them, so I assume I could do that..

I observed there 2 big issues here.
Music (for now i am using only 1 track) and animations.

Some of my animation are made from 40-50 files (400x200 pixels / file)
I assume these one are not the best practice..

So image = null it's correct to be used in order to free the image, right?


TomToad(Posted 2013) [#4]
When you null the image, the resources are freed, but not necessarily right away. As Yasha said, BMax can hang onto the space for quite a while, possibly till the end of the program. You can try GCCollect() to force the GC to free memory. It may take multiple calls as GCCollect doesn't seem to collect everything all at once. Maybe add the below to your program, when memory usage gets large, press the hotkey and see if memory usage drops after a few seconds.
If KeyHit(KEY_F1)
    For Local GCLoop:Int = 1 To 3 
        GCCollect()
    Next
End If



TomToad(Posted 2013) [#5]
Just had one more thought to consider. Images tend to be expanded when loading into Blitzmax. A 100K, 10 frame animated png image can possibly end up being 500K by the time it is loaded into memory. If you load in enough images, it could possibly be taking up 800 MB even if it only occupies 100MB on your hard drive.


xlsior(Posted 2013) [#6]
Same for music, it gets uncompressed in memory.


Ravl(Posted 2013) [#7]
I made some optimization.

I had some minigames. After completing them now I free those images, too. Also I am calling the CGCollect after these minigames.

I played the whole game. Except the scenes with big animation, I have between 300-400 mb.. I think it is ok for now.