4

I am working on performance improvement of my ios cocos2d game. I was checking memory allocations of the app with the help of Instruments tool when I noticed one thing. There are too many CFString objects being declared and held by [NSBundle mainBundle] call. It says,

Category: CFString (immutable) Responsible Caller: [NSBundle mainBundle]

There are many places in my code where I wrote following lines

[[NSBundle mainBundle] pathForResource:@"resource-name" ofType:@"png" isDirectory:imageDirectory];

Is this CFString problem is because of above code because I am giving a hard coded string in pathForResource method? Or what can be reason of this issue? Can anyone please help? This CFString allocations is taking about 2Mb of my code so I am worried about it.

Best Regards

Aqueel
  • 1,246
  • 3
  • 23
  • 46

2 Answers2

4

These CFString's are due to having a large number of resources in your application bundle. In my testing I found 1 CFString allocated for each file at the root of the bundle. Presumably this is some kind of caching of path names.

I am currently working on an app with 1,000's of resources in the bundle and these immutable strings are taking up ~ 300K. When I remove the majority of them, I wind up with about 20K, with around 100 CFStrings for ~ 80 resources in the bundle.

It seems like the answer to reducing these is to put Resources in sub-directories within the bundle. You can use a "Folder Reference" in Xcode to do this.

For example, you might have 1,000 PNGs for your game. Put those in a folder called "Assets" in your project (on disk). Drag the "Assets" directory to Xcode and instead of creating a Group, create a Folder Reference.

Jess Bowers
  • 2,846
  • 1
  • 22
  • 42
  • Hi Jess thanks for answer. Please explain how this will reduce number of CFString objects? – Aqueel Feb 17 '12 at 13:57
  • There appears to be 1 CFString for each path in the root directory of the bundle. Reducing the number of files at the root reduces the number of CFStrings. – Jess Bowers Feb 22 '12 at 15:49
  • Thanks a lot Jess. I would definately try that and will let you guys know. Thanks once again. Can you please tell exactly what path is generated and why it is not generated for the resources in sub folders? – Aqueel Apr 18 '12 at 15:30
  • I have no idea, that gets into the internals of iOS – Jess Bowers Apr 19 '12 at 01:08
1

No, that's not why NSBundle is allocating strings, and no, you're not doing anything wrong there. It seems extraordinarily unlikely that -[NSBundle mainBundle] is actually allocating 2MB of strings, so I suggest you look at some of the other allocation stack traces and see if you can find the real culprit.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
  • so if there is Category: CFString (immutable) and Responsible Caller: [NSBundle mainBundle] then what exactly does this mean? Because this is what I am noticing in Instruments. – Aqueel Jan 26 '12 at 05:54
  • Are you looking at the full stack trace in the (by default collapsed) right sidebar, or just Instrument's best guess in the tableview? – Catfish_Man Jan 26 '12 at 17:56