简体   繁体   English

没有内存泄漏,但内存仍然不足吗?

[英]No memory leaks, yet still running out of memory?

In my cocos2d application I have run through it using instruments and removed all memory leaks. 在我的cocos2d应用程序中,我已经使用仪器进行了遍历,并消除了所有内存泄漏。 I have my game outputting how much memory is in use on the screen and it is constantly rising as the game progresses until I eventually run out of memory. 我让游戏输出屏幕上正在使用的内存量,并且随着游戏的进行不断增加,直到我最终用尽内存。 The amount of objects on screen doesn't increase by very much each level. 屏幕上的对象数量并没有逐级增加。 Its a fairly simple game so i should not be running out of memory so soon. 它是一个相当简单的游戏,所以我不应该这么快就耗尽内存。

I am removing all objects from a level when it ends and reallocating new ones when a new level begins. 我要从某个级别结束时删除所有对象,并在一个新级别开始时重新分配新对象。 Instruments tells me there are no memory leaks. 仪器告诉我没有内存泄漏。 When i run through instruments to show me where the allocations are the bulk of the problem doesn't seem to come from one place its all of my objects. 当我通过各种工具向我展示分配的主要问题时,似乎并不是我所有的对象都来自一个地方。

Any ideas what the issue might be? 任何想法可能是什么问题?

Just because instruments doesn't show leaks, doesn't mean you're not still allocating memory you're not using. 仅仅因为仪器没有显示泄漏,并不意味着您还没有分配未使用的内存。 Pay attention to the ObjectAlloc graph and see if that is constantly rising without falling off. 注意ObjectAlloc图,看看它是否一直在上升而不下降。

If you allocate memory that you aren't using and can't clean up, that's just as bad as a leak, even though you haven't technically leaked anything because you've stashed some unused reference somewhere. 如果您分配了未使用且无法清理的内存,那就像泄漏一样严重,即使您在技术上没有泄漏任何东西是因为您已经在某个地方藏了一些未使用的引用。

Look at how much memory you actually need to use or to keep for future use at any point in your app, then treat anything else previously allocated as a leak and fix it. 查看您在应用程序中的任何时候实际需要使用或保留以供将来使用的内存量,然后将先前分配的其他任何内容视为泄漏并加以修复。

In Objective-C when using autorelease objects be wary of circular dependencies! 在Objective-C中使用自动释放对象时,请注意循环依赖!

If you create object A and then an instance of object B and pass A to it, so that B retains A, and A also retains B (for example by adding it as child to A) you can easily setup a circular dependency and thus both objects won't be released. 如果先创建对象A,然后再创建对象B的实例,然后将A传递给它,以便B保留A,而A也保留B(例如,通过将其作为子对象添加到A),则可以轻松设置循环依赖关系,因此对象不会被释放。

Tip: add a -(void) dealloc method to all your scene classes, and set a breakpoint there. 提示:向您所有的场景类添加-(void)dealloc方法,并在其中设置一个断点。 If you change a scene and its dealloc method isn't called after the scene transition has finished, you're leaking this scene (it's not released). 如果您更改场景,并且场景转换完成后未调用其dealloc方法,则您正在泄漏该场景(未发布)。

Try to find that leak by looking for a setup that's similar to the one I described above. 尝试通过查找与我上面描述的设置类似的设置来查找该泄漏。

Be careful about autoreleased objects, since they are not released immediately. 请注意自动释放​​的对象,因为它们不会立即释放。 If you have sections with lots of allocations and autoreleasing, try using specific autorelease pools on them. 如果您的部分具有大量分配和自动释放功能,请尝试在其上使用特定的自动释放池。 It happened to me when creating huge decision trees (using NSArrays ) for a game AI. 当为游戏AI创建巨大的决策树(使用NSArrays )时,这件事发生了。

A great way to check for objects that are aggregating in memory is to use Instruments' new (in Xcode 3.2.3) Heap Shot functionality. 检查内存中正在聚合的对象的一种好方法是使用Instruments的新(在Xcode 3.2.3中)Heap Shot功能。

Use the normal Allocations instrument against your running application. 对正在运行的应用程序使用常规分配工具。 Perform a series of repetitive events that should come back to some known state (for example, go one level down in a navigation controller and come back). 执行一系列重复的事件,这些事件应返回到某些已知状态(例如,在导航控制器中下一层然后返回)。 Every time you do this, click on the Mark Heap button in the left sidebar for the Allocations instrument (under the section heading Heapshot Analysis ). 每次执行此操作时,请单击“分配”工具左侧栏中的“ Mark Heap按钮(在“ Heapshot Analysis标题下)。

What this will do is mark the heap at each of the starting points for this repetitive action and compare the objects that have been created by that point with the objects that had been created by the time you marked the heap last. 这样做是在此重复操作的每个起点处标记堆,并将该点创建的对象与上次标记堆时已创建的对象进行比较。 Only objects that have been created between those two points and are still alive in memory will be listed. 将仅列出在这两点之间创建并仍然存在于内存中的对象。

If you are accumulating objects, but they are either not leaks or are being missed by the Leaks tool, they should show up here. 如果您正在累积对象,但它们不是泄漏或被泄漏工具遗漏,则应在此处显示。 I've found a number of subtle memory buildups this way, particularly when you pair this with the UI Automation instrument to automate the repetitive actions you're testing. 我发现通过这种方式可以建立许多微妙的内存,特别是当您将其与UI Automation工具配对以自动执行要测试的重复动作时。

The bulk of the problem seem to be that Sprites don't get released in cocos2d unless all actions have been stopped on those sprites. 问题的大部分似乎是Sprites不会在cocos2d中释放,除非所有动作都已停止在这些sprites上。 This is done using stopAllActions. 这是使用stopAllActions完成的。 Cheers for all the suggestions. 为所有建议加油。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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