I have a program that reads a huge text file (line by line) and does some string operations with each line before writing the line into a database.
The program needed more and more memory so I figured that I might need to release the strings that I use. But it did not help. So I have put together the following code to test out what actually happens. With some trial and error I found out that when I do the drain on the autorelease pool it works.
I would like to know what I do. So I ask:
- Why is the release not releasing memory?
- Is there a better way to do this?
Here is my test program
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int cnt = 0;
while (cnt < 1000000000) {
NSMutableString *teststr = [[NSMutableString alloc] init];
teststr = [NSString stringWithString:@"Dummy string just for the demo"];
cnt++;
if (cnt % 1000000 == 0) {
printf("cnt=%i\n",cnt);
}
[teststr release];
// [pool drain]; // It works when I do this
// [[NSAutoreleasePool alloc] init]; // and this
}
[pool drain];
return 0;
}
EDIT: Based on the answers so far I have looked on my original program and changed the test program:
//teststr = [NSString stringWithString:@"Dummy string just for the demo"];
[teststr appendString:@"Dummy string just for the demo"];
Does this also create a new string? Because still I have the memory problem. My routine works in way that I append the string with something but maybe start with an empty string at the beginning.