Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top