0

There are lot of low-memory problems out there with Game Apps running on iPad devices. So I am thinking for my own game about the following solution: Before the App starts I alloc the required space and free it up immediately on start-up.

This seems to work very well, giving me more stability with memory allocation issues when the app is running. What do you think about it? Is it a good way to do this?

e.g:

{
    size_t size = 30*MB;

    NSLog(@"free up %u bytes", size);

    size_t allocated = 0;

    while(allocated < size && blocksCounter < MAX_BLOCKS) {
        const size_t blockSize = 256*KB;    // 256K blocks

        blocks[blocksCounter] = malloc(blockSize);

        if(blocks[blocksCounter]) {
            bzero(blocks[blocksCounter], blockSize);
        }
        else {
            NSLog(@"warning empty block at idx %d", blocksCounter);
        }

        blocksCounter++;
        allocated += blockSize;
    } 

    for(int i=blocksCounter-1; i>=0; i--) {
        if(blocks[i]) {
            free(blocks[i]);
        }
    }

    blocksCounter = 0;
}
ivicaa
  • 605
  • 1
  • 6
  • 17
  • Welcome to SO! Take a minute to check out the [FAQ](http://stackoverflow.com/faq), under "What questions should I *not* ask here." Particularly the third bullet point. Since your problem seems to already be solved, it becomes a bit subjective. – Stephen Feb 21 '11 at 17:18
  • Thanks for the pointer Stephen! I'll be keeping this in mind, when posting the next time! But about my post, I do not see it as a solution actually, hoping people tell me what to do instead, since the code above is kind of workaround. – ivicaa Feb 21 '11 at 20:07

1 Answers1

2

It is very bad practice. Because it is not a good user experience to force a low memory warning which leads to quitting the iPod app for example or stopping other running background applications such as skype or your navigation software.

I would watch your memory consumption more closely and work on that instead or using your elbows every time you start up your app. This is not good citizenship on iOS.

GorillaPatch
  • 5,007
  • 1
  • 39
  • 56
  • But if I free up only the space required for my App, which should be there in any case, I don't see it as elbow-technique. I know it is kind of workaround, but I have problems with AudioToolBox (running in a separate thread) not getting memory freed fast enough by iOS. First it crashes with malloc returning a 0 sized block, then the didReceiveMemoryWarning message occures. :-/ – ivicaa Feb 21 '11 at 19:45
  • 1
    Well do what you have to do. But this looks like quite a hack. Maybe consider going to the root of your memory problem you described here. Maybe file a bug on apple's bug reporter about your memory allocation problems. – GorillaPatch Feb 21 '11 at 22:47
  • 1
    I thought about what you said, and reconsidered my solution. It took me the whole day, but as you said, it is really a hack. The way to go was to preloaded all the textures at the App start. Now the problem is not occurring anymore and I have a minimal memory footprint for my App, being a good iOS citizen. ;-) – ivicaa Feb 22 '11 at 16:08
  • That sounds much better. There is nothing wrong with preloading textures in a local cache and emptying that cache if a low-memory warning occurs. Makes much more sense instead of just allocating memory for nothing. I was happy to help. – GorillaPatch Feb 23 '11 at 08:18