由 [NSBundle mainBundle] 声明的 CFString 对象
我正在致力于提高我的 ios cocos2d 游戏的性能。当我注意到一件事时,我正在借助 Instruments 工具检查应用程序的内存分配。 [NSBundle mainBundle] 调用声明和持有太多 CFString 对象。它说,
类别:CFString(不可变) 负责任的调用者:[NSBundle mainBundle]
在我的代码中有很多地方我写了以下几行,
[[NSBundle mainBundle] pathForResource:@"resource-name" ofType:@"png" isDirectory:imageDirectory];
这个 CFString 问题是因为上面的代码吗,因为我在 pathForResource 方法中给出了硬编码字符串?或者这个问题的原因是什么?有人可以帮忙吗?这个 CFString 分配占用了我大约 2Mb 的代码,所以我很担心。
此致
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些 CFString 是由于您的应用程序包中包含大量资源造成的。在我的测试中,我发现为捆绑包根目录下的每个文件分配了 1 个 CFString。据推测,这是某种路径名缓存。
我目前正在开发一个应用程序,捆绑包中包含 1,000 个资源,这些不可变字符串占用了约 300K。当我删除其中的大部分时,我最终得到了大约 20K,其中大约 100 个 CFString 用于捆绑中的约 80 个资源。
减少这些资源的答案似乎是将资源放在捆绑包内的子目录中。您可以使用 Xcode 中的“文件夹参考”来执行此操作。
例如,您的游戏可能有 1,000 个 PNG。将它们放在项目中(在磁盘上)名为“Assets”的文件夹中。将“Assets”目录拖到 Xcode,而不是创建组,而是创建文件夹引用。
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.
不,这不是 NSBundle 分配字符串的原因,不,你没有做任何错误的事情。 -[NSBundle mainBundle] 实际上分配 2MB 的字符串似乎极不可能,所以我建议您查看其他一些分配堆栈跟踪,看看是否能找到真正的罪魁祸首。
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.