Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [a5] Memory Management -Bitmaps

This thread is locked; no one can reply to it. rss feed Print
[a5] Memory Management -Bitmaps
Phrasz
Member #10,091
August 2008

So I've recently created a game that uses bitmaps and sound clips(from ogg files).

However, My program hemorrhages memory. I've been using a 1280x720 standard, and for simplicity's sake I've made a couple screens full screen to render the static back ground once.

I used some of the examples' models on loading a bit map to memory and then loading it as a allegro_bitmap.

#SelectExpand
1 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); 2 membitmap = al_load_bitmap("images/bg.png"); 3 if (!membitmap) { 4 abort_example("bg.png not found or failed to load\n"); 5 } 6 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); 7 bg =al_clone_bitmap(membitmap); 8 al_destroy_bitmap(membitmap);

I included the destroy_bitmap(membitmap) and seemed to have the same speed and less use of resources.

1. Now as a rule of thumb would I want to destroy the bitmap as soon as I done using it? (in the case above I destroy mimbitmap when it's cloned into the bg ALLEGRO_BITMAP .... then after I display the bg I destroy it as well )

2. If I have several objects to animate would it be better to animate the sequence and destroy upon completion, or is it better to have implemented a use and lose type structure (purge non-needed memory ASAP)

3. I currently use ALLEGRO_BITMAP. Is the only memory management needed the al_destroy_bitmap() function?

4. When using a loading bitmap would I destroy it after each call, or wait until its use is finished?

#SelectExpand
1 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); 2 membitmap = al_load_bitmap("images/bg.png"); 3 if (!membitmap) { 4 abort_example("bg.png not found or failed to load\n"); 5 } 6 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); 7 bg =al_clone_bitmap(membitmap); 8 al_destroy_bitmap(membitmap); 9 10 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); 11 membitmap = al_load_bitmap("images/bg2.png"); 12 if (!membitmap) { 13 abort_example("bg.png not found or failed to load\n"); 14 } 15 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); 16 bg2 =al_clone_bitmap(membitmap); 17 al_destroy_bitmap(membitmap);

Hopefully I can understand this issue, so I stop using 500MBs of ram to play a simple game...

Thanks in Advance!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Phrasz said:

However, My program hemorrhages memory.

How do you know this? Task Manager? Windows won't deallocate memory from your program unless it's needed somewhere else, that way it can re-use the same memory pages without constantly reallocating them.

You do know that you can load bitmaps directly to video memory right? Or is there some other reason you load them as memory bitmaps and then clone them into video bitmaps?

If you know you're not going to use a bitmap anymore, then it makes sense to free it.

Phrasz said:

I currently use ALLEGRO_BITMAP. Is the only memory management needed the al_destroy_bitmap() function?

Pretty much, yes.

Phrasz said:

When using a loading bitmap would I destroy it after each call, or wait until its use is finished?

I would wait until I was done using. Why load once for every time you use it unless this is done infrequently?

Phrasz
Member #10,091
August 2008

In answer to your first question: Yes. I'd pull up task manager and watch the memory use. Most of the code was fine, but some other parts would not destroy the memory properly, so when I'd keep changing options (showing the main menu vs the options screen) I'd see the memory keep rising.

As far as loading the bitmaps directly to video memory I figured I could, and believed I did this in old 4.2 days, but I just followed the example programs included with the source.

Finally, I'm glad to hear it's just the destroy bitmap function... I'm justing going to review my code to ensure it creates and destroys the bitmaps the most efficient way. (The current idea is create the bitmaps when entering the new case, in a switch/case, and destroy the memory usage before exiting the case.)

Thanks again!

Go to: