0

My co-worker and I are having a problem we are trying to fix. It has to do with memory allocation. Our code uses loops and componentsSeperateByString to split a large document by the character "\n". The weird thing is that this never returns all the memory it took. Or at least when we check it doesn't can someone look at the code below for the function and tell us what we might be doing wrong.

- (void)loadTagmeTest:(NSString *)selectedTargetName
{           
    NSString *path = [[NSBundle mainBundle] pathForResource:selectedTargetName ofType:@"tagme"]; 
    NSString *objData = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    NSArray *lines1;
    NSArray *lineVertices1;
    NSArray *lineNormals1;
    NSArray *lineTexcoords1;

    lines1 = [objData componentsSeparatedByString:@"\n"];


    // Iterate through file once to discover how many vertices, normals, and faces there are

    lines1 = [self getList:objData separator:@"\n"]; 


    for (NSString * line in lines1)
    {
        if ([line hasPrefix:@"Verts: "])
        {            

            lineVertices1 = [[line substringFromIndex:7] componentsSeparatedByString:@","];
        }
        else if ([line hasPrefix:@"Normals: "]){

            lineNormals1 = [[line substringFromIndex:9] componentsSeparatedByString:@","];
        }
        else if ([line hasPrefix:@"TexCoords: "]){

            lineTexcoords1 = [[line substringFromIndex:11] componentsSeparatedByString:@","];
        }
    }

}

Edit: This is no longer a pressing matter as we rolled our own version of the parsing function, so now the code uses less memory, actually returns the memory it uses after it is done, and is slightly faster to parse (a whole document (about 10,000 lines) parses and is transormed into floats, put into arrays etc, etc in about 100ms.

Although it is not a pressing concern i would like to know why the memory allocation of NSString is so bad (at least in our implementation above).

Thanks.

Raigex
  • 1,205
  • 12
  • 32
  • How are you measuring? And are you using ARC? – borrrden Sep 18 '12 at 14:14
  • I am measuring both with vm_statistics_data and how much available memory there is and using the leaks/allocations instrument to see how much memory is still taken even at the end of the function. – Raigex Sep 18 '12 at 14:17
  • What about the 2nd part of my question?? Either way I notice that you are still keeping around a reference to 3 instance arrays. – borrrden Sep 18 '12 at 14:19
  • Sorry no he I am not using ARC. what do you mean I am keeping reference to 3 instance arrays. as far as I know they should be auto released – Raigex Sep 18 '12 at 14:21
  • Right, you are correct if you are not using ARC (under ARC they would be retained). What happens if you remove the final loop? If you set your array contents to nil before they are released then they will never actually be released (an NSArray's behavior is to retain the objects inside, and release them when it is released or when the object is removed from the array in the case of a mutable array). – borrrden Sep 18 '12 at 14:28
  • the final loop is not used, we have it commented out(the above code is a couple minutes older than what we are using). And checking the memory usage shows that we start with 240mb of space available then after loading once it drops down to 210mb then 198mb and just keeps going down, with no releases of memory – Raigex Sep 18 '12 at 14:38
  • The next step, then, is to share what is inside [self getList] – borrrden Sep 18 '12 at 14:41
  • its a custom parsing function, not used because it takes waaaay too much memory. – Raigex Sep 18 '12 at 14:45
  • 1
    Dude...which part of your code IS used then? -____- Edit your question to get rid of stuff that wastes people's time. – borrrden Sep 18 '12 at 14:47
  • ok just stuff that is used currently is in the source code. – Raigex Sep 18 '12 at 14:49

1 Answers1

0

I have not found the answer to this question but I have created a work around, that is a bit faster in what I need. I use base C code and memcpy to parse my file into what I need bypassing the weird actions of NSString seperate that happen on my devices.

Raigex
  • 1,205
  • 12
  • 32