0

I have a memory problem with this loop. I use ARC, so normally I cannot release any objects. But during this loop process, the memory grow up and the iPad crash after a moment (without warning and error).

Is there a solution to solve this problem? I don't understand why the memory grow up?

Thank you for your help.

_rawdatalocal = [NSEntityDescription insertNewObjectForEntityForName:@"RAWDATA"
                                                       inManagedObjectContext:managedObjectContext];    

for(int j = 1; j <[array count];j++)
        {
            NSArray *values = [[NSArray alloc] initWithArray:[[array objectAtIndex:j] componentsSeparatedByString:@";"]];

            if([values count] == 12)
            {
            _rawdatalocal.accX = [[NSNumber alloc] initWithInt:[[values objectAtIndex:1] intValue]];
                _rawdatalocal.accY = [[NSNumber alloc] initWithInt:[[values objectAtIndex:2] intValue]];
                _rawdatalocal.accZ = [[NSNumber alloc] initWithInt:[[values objectAtIndex:3] intValue]];
                _rawdatalocal.gyrX = [[NSNumber alloc] initWithInt:[[values objectAtIndex:4] intValue]];
                _rawdatalocal.gyrY = [[NSNumber alloc] initWithInt:[[values objectAtIndex:5] intValue]];
                _rawdatalocal.gyrZ = [[NSNumber alloc] initWithInt:[[values objectAtIndex:6] intValue]];
                _rawdatalocal.magX = [[NSNumber alloc] initWithInt:[[values objectAtIndex:7] intValue]];
                _rawdatalocal.magY = [[NSNumber alloc] initWithInt:[[values objectAtIndex:8] intValue]];
                _rawdatalocal.magZ = [[NSNumber alloc] initWithInt:[[values objectAtIndex:9] intValue]];
                _rawdatalocal.temperature = [[NSNumber alloc] initWithFloat:[[values objectAtIndex:10] floatValue]];
                _rawdatalocal.temps = [[NSNumber alloc] initWithInt:[[values objectAtIndex:11] intValue]];

        }

PS : Normally the line below is in the loop

_rawdatalocal = [NSEntityDescription insertNewObjectForEntityForName:@"RAWDATA"
                                                           inManagedObjectContext:managedObjectContext];
  • Do you really need to use NSNumber ? If you are not trying to convert anything in your code, stick with a simple int or long. It will use less memory and be faster to initialize. I don't know how many loops you have, but it can be significant if you say that it creates memory crashes. Profile your app and tell us about the memory space – Kalzem Feb 25 '13 at 18:07

1 Answers1

1

You can put an autorelease pool inside your loop to avoid memory spikes. In arc, you need to put your autorelease polls as following:

@autorelease{

}

More information here

J2theC
  • 4,412
  • 1
  • 12
  • 14