2

I've been trying to make some simulation calculations in an iOS App, but my phone gets out of memory over time, it just doesn't stop adding memory usage and although I know where this happens (nailed it with Instruments) I still don't know what I need to change to stop the memory leakage.

Here's the method where the memory allocation happens, and I know that I'm adding new data here but I thought ARC will release all the allocated data that I can't reference any more?

int round = 0;

InstanceInSimulation *simulatingChosen = [[InstanceInSimulation alloc] initWithSimulationInstance:_chosenInstance];
InstanceInSimulation *simulatingOpponent = [[InstanceInSimulation alloc] initWithSimulationInstance:_opponentInstance];

while (round < _maxRounds) {

    // first choose action
    NSManagedObject *chosensAction = [simulatingChosen nextActionAgainstOpponent:simulatingOpponent];
    NSManagedObject *opponentsAction = [simulatingOpponent nextActionAgainstOpponent:simulatingChosen];

    // second calculate which action first
    InstanceInSimulation *attackingFirst;
    InstanceInSimulation *defendingFirst;
    NSManagedObject *attackingFirstsAction;
    NSManagedObject *defendingFirstsAction;

    if (simulatingChosen.instance.speed > simulatingOpponent.instance.speed) {
        attackingFirst = simulatingChosen;
        defendingFirst = simulatingOpponent;
        attackingFirstsAction = chosensAction;
        defendingFirstsAction = opponentsAction;
    } else {
        attackingFirst = simulatingOpponent;
        defendingFirst = simulatingChosen;
        attackingFirstsAction = opponentsAction;
        defendingFirstsAction = chosensAction;
    }

    // memory savings
    chosensAction = nil;
    opponentsAction = nil;

    // third calculate
    [self calculateSomething]; // this is not the memory problem

    // memory savings
    attackingFirst = nil;
    attackingFirstsAction = nil;
    defendingFirst = nil;
    defendingFirstsAction = nil;

    round++;
}

// memory savings
simulatingChosen = nil;
simulatingOpponent = nil;

Do I need to do some __weak stuff? I don't know how, could someone please help me with that?

2 Answers 2

5

I am not sure this will help but it's worth a try. Try putting an autoreleasepool inside you loop.

while (round < _maxRounds) {

    @autoreleasepool
    {
    ...
    }
}
5
  • Thanks a lot, this immediately dropped my memory usage by 80 %! I'll try to fix the lasting 20 % as well, think I will just need to add autorelease pools in other loops as well. Thanks! :)
    – CGee
    Jul 15, 2013 at 12:30
  • Uhm, one more question: can I now get rid of the lines that are setting objects to nil? Or are they still helping in any way?
    – CGee
    Jul 15, 2013 at 12:46
  • Yes, you can drop those lines. Jul 15, 2013 at 12:51
  • OK, I will. By the way, I now added autoreleasepools in many of my loops, I hope they don't have a big disadvantage. The memory usage has dropped even by 92 % now! :)
    – CGee
    Jul 15, 2013 at 12:55
  • wow. I don't think there is much disadvantage to creating autorelease pools in your loops. I think it might affect the performance a little since the pool is being drained in every iteration. But I think you should use them if you have a lot of autoreleased objects in your loops. Jul 15, 2013 at 13:02
1

What you are seeing are not memory leaks, the memory that is accumulating is still referenced by the autorelease pool. You are creating auto-released objects in your while loop but the autorelease pool is never drained.

Like Ahmed suggested, create a new autorelease pool in your loop:

while (round < _maxRounds) {
    @autoreleasepool {
         // your code here
    }
}

Have a look at Using Autorelease Pool Blocks in the Advanced Memory Management Programming Guide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.