简体   繁体   中英

How can I keep memory usage low with a lot of NSStrings, or how do I release NSStrings?

I am working on an app that imports a (very) large csv file into Core Data for catching purposes. The process goes something like this:

  1. Parse the file line-by-line into an NSMutableArray full of NSStrings
  2. After X number of lines, import the array into Core Data (this involves manipulating and in some cases creating new NSStrings)
  3. [NSMutableArray removeAllObjects];
  4. Rinse and repeat

At first glance it looks like the memory should be freed up at the conclusion of each cycle. With large files, however, I am finding that the app crashes after signaling a couple low memory warnings. Running the leaks tool tells me that most of the memory is being used up by CFString objects, which I understand are related to NSString objects (although I don't know how)

I understand that NSString are reused whenever possible, and that they don't act quite the same as other objects where memory is concerned, although I don't understand any of the details. How can I reclaim the memory that my NSString objects are using?

I am assuming that you don't have memory leak.

If you are using too much autoreleased object this can happen

You try following

  1. Create nested auto release pools --- some time happen that you have some long running loops where auto release object get accumulated. -- so add custom auto release pool to release the auto release object when you required.

  2. Don't use autorelease object in the parsing cycle --- do manual object allocation and release once your work is done.

Running the leaks tool tells me that most of the memory is being used up by CFString objects, which I understand are related to NSString objects (although I don't know how)

NSString is actually a class cluster. Although you think you are working with NSStrings, you are almost certainly really working with one of its subclasses. The Cocoa framework chooses which subclass to use depending on circumstances.

CFString is actually not really an NSString at all, it is the pure C string object used by Core Foundation . However, you'll find it is "toll free bridged" to NSString. This means that, to Cocoa, it looks like an NSString. The reason you are seeing lots of CFString usage is because whatever Cocoa API you are using to obtain these strings ultimately performs its work in Core Foundation.

Anyway, all that is irrelevant to your problem except for the fact, that lots of CFStrings more or less means the same as lots of NSStrings. What you need to reduce your memory footprint is nested autorelease pools as Girish has already said. As a first step, modify your algorithm like this:

  1. Create a new autorelease pool.
  2. Parse the file line-by-line into an NSMutableArray full of NSStrings
  3. After X number of lines, import the array into Core Data (this involves manipulating and in some cases creating new NSStrings)
  4. [NSMutableArray removeAllObjects];
  5. drain the autorelease pool
  6. Rinse and repeat (start at 0)

If this doesn't help, or only helps a bit, consider bracketing just the parsing with an autorelease pool.

You can't sure that the memory is consumed by NSStrings only. I suggest you check it thoroughly. Try to go for "Build and Analyze" it will help you to find out leaks.

While using NSString object, instead of going for autoreleased objects like

[NSString stringWithFormat@""];

create your own object and release it as soon as you done with it.

NSString * string = [[NSString alloc]initWithFormat:@""];
//use string object
[string release];

this way you can be sure, that you releasing the string there itself.

or create an Autorelease pool

NSAutoReleasePool * pool = [[NSAutoReleasePool alloc]init];
// do you coding, creation of objects, releasing them, whatever...
[pool drain]; or [pool release];

Also have a look at these memory management tips

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM