CFString Objects Declared [NSBundle mainBundle]

I am working on improving the performance of my ios cocos2d game. I checked application memory allocation using the Tools tool when I noticed one thing. Too many CFString objects are declared and held by calling [NSBundle mainBundle]. It says:

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

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

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

Is this CFString problem due to the above code because I am giving a string encoded string in the pathForResource method? Or what could be causing this problem? Can anybody help? This CFString distribution takes about 2 MB of my code, so I'm worried about it.

Best wishes

+4
source share
2 answers

These CFString are associated with the presence of a large number of resources in your application. In my testing, I found 1 CFString allocated for each file in the root directory. Presumably this is some kind of path name caching.

I am currently working on an application with 1000 resources in a bundle, and these immutable lines occupy ~ 300 KB. When I delete most of them, I finish about 20KB with about 100 CFStrings for ~ 80 resources in the kit.

It seems that the answer to their reduction is to put resources in subdirectories in the package. For this, you can use the "Folder Reference" in Xcode.

For example, you may have 1000 PNGs for your game. Place them in the Assets folder in your project (on disk). Drag the Assets directory into Xcode and create a link to the folder instead of creating a group.

+4
source

No, that’s not why the NSBundle highlights the lines, and no, you are not doing anything wrong there. It seems incredibly unlikely that [NSBundle mainBundle] actually allocates 2 MB of lines, so I suggest you look at some other traces of the distribution stack and see if you can find the real culprit.

+1
source

Source: https://habr.com/ru/post/1392946/


All Articles