views:

16

answers:

2

iPad application

I have a large text file which I want to divide into several pieces and process them in a loop. I used the following code:

NSArray * contentArray = [stringFromFile componentsSeparatedByString:@" "];

for(...){
    contentRange = NSMakeRange(lastPosition , newLength);
    NSArray * subContentArray = [contentArray subarrayWithRange:contentRange];
    NSString *subContent = [subContentArray componentsJoinedByString:@" "];
    ....
    //Process sub content here...

}

After running, I receive malloc error, code 12

Observing the Activity monitor, the memory and the VM size increases and until system memory is exhausted and the application crashes.

Is there any way to prevent this?

+2  A: 

One way to work around this issue will be to use a custom NSAutoreleasePool to constantly clear temporarily allocated memory. Like so:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUInteger i=0;

for (...) {
  ... // your method contents

  if (++i % 10 == 0) {
    // you might want to play with the frequency of the releases, depending on the size of your loop
    [pool release];
    pool = [[NSAutoreleasePool alloc] init];
  }
}

[pool release];
Max Seelemann
+1, except that autorelease pools are cheap. Just make one on every loop and avoid the whole mod operator dancing.
Dave DeLong
Thank you, worked like a charm. I was next to sacrifice a black chicken.
Mohammed Osman
@Dave: right, you are.
Max Seelemann
A: 

The autorelease pool thing will take care of the memory problem for the most part, but if the file is any size it might be slow. It seems like scanning the original string in a loop getting substrings directly from the string would be faster. Also use at least 1/2 the memory, since you duplicate the string in the components array.

The code would use rangeOfString on the original string, until a substring of appropriate length was found, then process the substring, and onto the next one.

iPhone 3G phones only have about 5 - 15 MB of memory total for your application. You are on an iPad, though I see which does give you about 4x that amount.

Tom Andersen
Thanks for the hint, I will try to git rid of the subArrayWithRange which is obviously causing the problem. I will try to use rangeOfString but I want to use words not chars.
Mohammed Osman