开发者

Retain/Release with stringByReplacingCharactersInRange Question

I was observing my memory footprint in Instruments and noticed a huge spike when I call the method below. The method performs a great deal of string manipulations and I'm wondering if I need to be retaining/releases NSStrings. If anyone sees anyway to improve this method please let me know.

-(void)findWords:(NSString *)query {
     //Removes Characters that aren't approved by me
     NSString *usersLetters = [query substringWithRange:allLetters];
     NSString *userLettersBackup = [query substringWithRange:allLetters];

     //Loop over all words in a NSD开发者_运维问答ictionary
     for(NSString *word in wordSection)
     {
        int wordLength = [word length];

        //Loop Over each char in the word
        for (int i = 0; i < wordLength; i++)
        {
          char current = [word characterAtIndex:i];

           //Loop Over each char in the usersletters 
           for (int indexUser = 0; indexUser <[usersLetters length]; indexUser++)
           {
             char compare = [usersLetters characterAtIndex:indexUser];
             if(current == compare){
                //Is this losing reference to the other userLetters?
                //and retaining the previous value?
                usersLetters = [usersLetters stringByReplacingCharactersInRange:NSMakeRange(indexUser, 1) withString:@"*"];
             } 
           }
        }
        //Reset the UserLetters for looking at the next word
        usersLetters = userLettersBackup;
      }
 }


Your spike is probably coming from the large amount of autoreleased objects that are getting created. In terms of memory management, I don't see anything that's immediately wrong.

You could alloc/init an NSAutoreleasePool at the beginning of the method and drain it at the end.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜